Java – how do I change the actionbar Sherlock menu item font when using a custom theme for actionbar?

When I use the default Sherlock light theme, I can change the font by this method (I can cast it to 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;
}

However, when I use the custom theme of this tool, the above solution does not work In this way, each item is an instance of actionmenuitemview. I don't know how to apply fonts to it

Solution

You need to create a custom menu view

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; 
 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>