在onCreateOptionsMenu中修改activity的menu颜色

记录一下开发遇到的问题:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        LayoutInflater lInflater = getLayoutInflater();
        if ( lInflater.getFactory() == null ) {
            lInflater.setFactory(new LayoutInflater.Factory() {
                @Override
                public View onCreateView(String name, Context context, AttributeSet attrs) {
                    System.out.println(name);
                    if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
                            || name.equalsIgnoreCase("com.android.internal.view.menu.ActionMenuItemView")) {
                        try {
                            LayoutInflater f = getLayoutInflater();
                            final View view = f.createView(name, null, attrs);
                            System.out.println((view instanceof TextView));
                            if(view instanceof TextView){
                                //设置要修改的颜色
                                ((TextView)view).setTextColor(getResources().getColor(R.color.action_barcolor));
                            }
                            return view;
                        } catch (InflateException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                }
            });
        }
        getMenuInflater().inflate(R.menu.finish, menu);
        menu.findItem(R.id.create_ok).setTitle("确定");
        return super.onCreateOptionsMenu(menu);
    }

你可能感兴趣的:(menu,menuItem‘’,android)