Android Menu item font using LayoutInflaterCompat.setFactory

别来无恙 提交于 2019-12-05 20:06:52

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

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

are you using fragment?

if you are then you must call following method in "onCreateView"

setHasOptionsMenu(true);

it just acknowledge to system that this fragment has its own option menu.

it worked for me.

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