加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在为actionbar使用自定义主题时更改actionbarsherlo

发布时间:2020-12-15 03:10:41 所属栏目:Java 来源:网络整理
导读:当我使用默认的sherlock light主题时,我可以通过此方法更改字体(可以将其强制转换为TextView) @Overridepublic boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main,menu); getLayoutInflater().setFactory(new LayoutI
当我使用默认的sherlock light主题时,我可以通过此方法更改字体(可以将其强制转换为TextView)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main,menu);

    getLayoutInflater().setFactory(new LayoutInflater.Factory() {
        public View onCreateView(String name,Context context,AttributeSet attrs) {

            if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
                    || name.equalsIgnoreCase("TextView")) {
                try {
                    LayoutInflater li = LayoutInflater.from(context);
                    final View view = li.createView(name,null,attrs);
                    new Handler().post(new Runnable() {
                        public void run() {

                            // here I can change the font!
                            ((TextView)view).setTypeface(MY_CUSTOM_TYPE_FACE);
                        }
                    });
                    return view;
                } catch (InflateException e) {
                    // Handle any inflation exception here
                } catch (ClassNotFoundException e) {
                    // Handle any ClassNotFoundException here
                }
            }
            return null;
        }
    });

    return true;
}

但是当我使用this tool的自定义主题时,上述解决方案不起作用.这样每个项目都是ActionMenuItemView的一个实例,我不知道如何将字体应用于它.

解决方法

需要创建自定义菜单视图
private ActionBar setCustomView(ActionBar actionBar,String title,String subtitle,boolean isSubTitleEnable){
    LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.customer_view,null);

    TextView tv = (TextView) v.findViewById(R.id.cust_action_bar_title);
    Typeface tf = Typeface.createFromAsset(this.getAssets(),"YOURFONT.ttf");
    tv.setTypeface(tf);
    tv.setText(title);


    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayOptions(0,actionBar.DISPLAY_SHOW_TITLE);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(v);
  return actionBar; 
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读