如何在工具栏上的溢出菜单中的菜单项上设置字体 [英] How to set a typeface to menu items in overflow menu on toolbar

查看:118
本文介绍了如何在工具栏上的溢出菜单中的菜单项上设置字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改溢出菜单的各项的默认字体并设置自定义字体.

我尝试将Factory添加到LayoutInflater,并在onCreateView()方法中更改了 TextView的字体.但这没有用.这是代码(在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;
        }
    });

实际上,当我调试代码时,它从未到达onCreateView()内部.

那该如何解决呢?

谢谢.

解决方案

这就是我解决此问题的方式,

第1步:创建一个全局Factory变量,并在如下所示的onCreate()中初始化

  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;
        }
    };

第2步:覆盖AppCompatActivity中的 onCreateView()方法. 请记住,有两个具有不同签名的oncreate()方法.

public View onCreateView(字符串名称,上下文上下文,AttributeSet属性)-适用于HONEYCOMB之前的应用程序

public View onCreateView(查看父级,字符串名称,上下文上下文,AttributeSet属性)-已在API 11中添加.

onCreateView的实现如下所示,

@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);
}

注意:

(1)我使用了如何为选项"菜单设置字体?

使用LayoutInflaterCompat.setFactory的Android菜单项字体

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.

解决方案

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

这篇关于如何在工具栏上的溢出菜单中的菜单项上设置字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆