我正在尝试让我的片段中的菜单更具定制化。代码在我创建的Button-Symbol上做了一些调整(顺便说一下,有没有更好的解决方案?)按钮的文本和字体也是自定义的。
代码在独立的活动中运行,在片段中运行它不再起作用:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container,true);
final Button events = view.findViewById(R.id.events);
// BUTTON FONT THEME
String etext = "<font color=#FFFFFF>NEXT</font> <font color=#8def00> EVENTS </font>";
if (Build.VERSION.SDK_INT >= 24) {
events.setText(Html.fromHtml(etext, 0)); // for 24 api and more
} else {
events.setText(Html.fromHtml(etext)); // or for older api
}
events.setTypeface(ralewayfont);
Drawable iconevents = ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_calendar);
int WIconCal = iconevents.getIntrinsicWidth();
int HIconCal = iconevents.getIntrinsicHeight();
iconevents.setBounds(0,0,WIconCal/3,HIconCal/3);
events.setCompoundDrawables(iconevents, null, null, null);
}
它没有给我任何错误-它不会调整图像的大小,也不会在按钮上设置自定义字体。
发布于 2018-07-31 05:07:51
您应该制作更多针对特定API版本的布局文件。在您的示例中,我将创建一个/res/layout-v24/fragment_main.xml
并在其中设置特定于v24的按钮属性。不需要做你在代码片段中做的所有事情。只需像你所做的那样扩大布局,用findViewById()连接你的按钮,如果安卓操作系统检测到使用v24或更高版本的手机,它将自动知道使用布局-v24版本。v24以下的任何内容都将使用您的默认/res/layout/fragment_main.xml
。
希望这能有所帮助!
https://stackoverflow.com/questions/51601972
复制相似问题