首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >片段中按钮上的自定义字体和图标

片段中按钮上的自定义字体和图标
EN

Stack Overflow用户
提问于 2018-07-31 04:39:59
回答 1查看 44关注 0票数 0

我正在尝试让我的片段中的菜单更具定制化。代码在我创建的Button-Symbol上做了一些调整(顺便说一下,有没有更好的解决方案?)按钮的文本和字体也是自定义的。

代码在独立的活动中运行,在片段中运行它不再起作用:

代码语言:javascript
复制
@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);
}

它没有给我任何错误-它不会调整图像的大小,也不会在按钮上设置自定义字体。

EN

回答 1

Stack Overflow用户

发布于 2018-07-31 05:07:51

您应该制作更多针对特定API版本的布局文件。在您的示例中,我将创建一个/res/layout-v24/fragment_main.xml并在其中设置特定于v24的按钮属性。不需要做你在代码片段中做的所有事情。只需像你所做的那样扩大布局,用findViewById()连接你的按钮,如果安卓操作系统检测到使用v24或更高版本的手机,它将自动知道使用布局-v24版本。v24以下的任何内容都将使用您的默认/res/layout/fragment_main.xml

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51601972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档