How to set a typeface to menu items in overflow menu on toolbar

牧云@^-^@ 提交于 2019-12-10 22:13:34

问题


I want to change the default typeface of the items of the overflow menu and set a custom typeface.

I tried adding a Factory to the LayoutInflater and within the onCreateView() method I changed the TextView's typeface. But it didn't work. Here is the code(Inside onCreateOptionsMenu),

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

            if (name.contains("com.android.internal.view.menu.IconMenuItemView")) {
                try {
                    LayoutInflater li = LayoutInflater.from(context);
                    final View view = li.createView(name, null, attrs);
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView) view).setTypeface("custom_typeface");
                        }
                    });
                    return view;

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    });

Actually when I debug the code, it never reached inside onCreateView().

So how to fix this?

Thanks.


回答1:


This is how I solved this issue,

step 1: create a global Factory variable and initialized in onCreate() like below,

  mFactory = new LayoutInflater.Factory() {
        @Override
        public View onCreateView(String name, final Context context, AttributeSet attrs) {
                try {
                    LayoutInflater li = LayoutInflater.from(context);
                    final View view = li.createView(name, null, attrs);

                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView) view.findViewById(R.id.title)).setTypeface("custom_typeface");
                        }
                    });

                    return view;

                } catch (Exception e) {
                    e.printStackTrace();
                }
            return null;
        }
    };

Step 2: override the onCreateView() method in AppCompatActivity. remember there are 2 oncreate() methods with different signatures.

public View onCreateView (String name, Context context, AttributeSet attrs) - for pre HONEYCOMB apps

public View onCreateView (View parent, String name, Context context, AttributeSet attrs) - added in API 11.

The implementation of onCreateView is like below,

@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {

    if(name.contains("android.support.v7.view.menu.ListMenuItemView")) {
        LayoutInflater li = LayoutInflater.from(context);
        View view = null;
        try {
            view = li.createView(name, null, attrs);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (view != null) {
            if(mFactory != null) {
                view = mFactory.onCreateView(name,context,attrs);
            }
            return view;
        }
    }
    return super.onCreateView(name, context, attrs);
}

Note:

(1) I used android.support.v7.view.menu.ListMenuItemView instead of com.android.internal.view.menu.IconMenuItemView since I am using the AppCompat support library.

(2) Since I have already initialized a Factory object in onCreate(), I removed the code segment(posted in my question) from onCreateOptionsMenu(). So it contains only this part,

 getMenuInflater().inflate(R.menu.my_menu, menu); 
 return true;

References :

How to set a font for the Options menu?

Android Menu item font using LayoutInflaterCompat.setFactory



来源:https://stackoverflow.com/questions/35551787/how-to-set-a-typeface-to-menu-items-in-overflow-menu-on-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!