使用Android的LayoutInflaterCompat

使用Android的LayoutInflaterCompat

本文介绍了使用Android的LayoutInflaterCompat.setFactory菜单项的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想改变的菜单项字体。根据,我使用 LayoutInflaterCompat.setFactory (支持库22.1.1是我的项目中使用)。我的code是这样的:

I'm trying to change font of Menu items. According to this answer, I'm using LayoutInflaterCompat.setFactory (support library 22.1.1 is used in my project). My code looks like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_income_mail, menu);
    mFilterMenu = menu.getItem(1).getSubMenu();

    final LayoutInflater inflaterCopy = getLayoutInflater().cloneInContext(this);
    LayoutInflaterCompat.setFactory(inflaterCopy, new LayoutInflaterFactory() {

        @Override
        public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
            // my code
        }
    });
    return true;
}

但方法 onCreateView(查看父,字符串名称,上下文的背景下,ATTRS的AttributeSet)永远不会被调用。我应该怎么改?

but method onCreateView(View parent, String name, Context context, AttributeSet attrs) is never called. What should I change?

推荐答案

您必须调用工厂手动从活动onCreateView内onCreateView。因为活动的onCreateView默认返回空值,所以如果你想其他明智的,你可以像这样

you have to call the factory onCreateView manually from within your activity onCreateView. because activity's onCreateView returns null by default so if you want other wise you can do like this

@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    if(name.contains("ActionMenuItemView")) {
        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);
}

这将检查是否LayoutInflater可以创建视图然后触发工厂onCreateView进行编辑

which will check if LayoutInflater can create the view then trigger the factory onCreateView to edit it

这篇关于使用Android的LayoutInflaterCompat.setFactory菜单项的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:38