How to change Menu Item Color & Size programmatically?

前端 未结 3 993
鱼传尺愫
鱼传尺愫 2020-12-03 20:13

I searched a lot in google and found in stackoverflow one link how to change color of text using styles and themes but I dont know how to use in code.



        
3条回答
  •  广开言路
    2020-12-03 20:44

    try this code to change background and text color....

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, menu);
        getLayoutInflater().setFactory(new Factory() {
        @Override
        public View onCreateView(String name, Context context,
                        AttributeSet attrs) {
                    if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                    try {
                        LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                                public void run() {
                                // set the background drawable
                                    view.setBackgroundResource(R.drawable.my_ac_menu_background);
    
                                // set the text color
                                    ((TextView) view).setTextColor(Color.WHITE);
                                }
                            });
                            return view;
                        } catch (InflateException e) {
                        } catch (ClassNotFoundException e) {
                        }
                    }
                    return null;
                }
            });
            return super.onCreateOptionsMenu(menu);
        }
    

提交回复
热议问题