工具栏溢出自定义字体

时间:2016-03-01 07:57:01

标签: java android overflow toolbar

我有一个带有工具栏和溢出菜单的应用程序就像这样..

overflow pic

如何更改工具栏溢出“刷新”,“设置”的文本字体  和另一个自定义字体?

1 个答案:

答案 0 :(得分:0)

以下是您搜索的答案:

public boolean onCreateOptionsMenu(android.view.Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.cool_menu, menu);
    getLayoutInflater().setFactory(new Factory() {
        public View onCreateView(String name, Context context,
                AttributeSet attrs) {

            if (name.equalsIgnoreCase(
                    "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() {
                        public void run() {
                            // set the background drawable if you want that
                            //or keep it default -- either an image, border
                            //gradient, drawable, etc.
                            view.setBackgroundResource(R.drawable.myimage);
                            ((TextView) view).setTextSize(20); 

                            // set the text color
                            Typeface face = Typeface.createFromAsset(
                                    getAssets(),"OldeEnglish.ttf");     
                            ((TextView) view).setTypeface(face);
                            ((TextView) view).setTextColor(Color.RED);
                        }
                    });
                    return view;
                } catch (InflateException e) {
                    //Handle any inflation exception here
                } catch (ClassNotFoundException e) {
                    //Handle any ClassNotFoundException here
                }
            }
            return null;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

来源:https://stackoverflow.com/a/11376591/5250273

但是,如果您想要一个更简单的解决方案,则应该查看此库:https://github.com/chrisjenx/Calligraphy

相关问题