java – 如何在为actionbar使用自定义主题时更改actionbarsherlock菜单项字体?

当我使用默认的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; 
 }

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...