How to change Menu Item Color & Size programmatically?

前端 未结 3 983
鱼传尺愫
鱼传尺愫 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:24

    @ 0101100101 just change the menu.IconmenutItem to menu.ActionMenuItemView

    0 讨论(0)
  • 2020-12-03 20:36

    I am using the following. Put this method in your activity and call it in the onCreateOptionsMenu(Menu menu) method. You can instead of setting the backgroundResource just set a color... just type view.setbackground and see the possibilities via autocompletion ;)

    /*
     * IconMenuItemView is the class that creates and controls the options menu
     * which is derived from basic View class. So We can use a LayoutInflater
     * object to create a view and apply the background.
     */
    protected void setMenuBackground() {
    
        Log.d(TAG, "Enterting setMenuBackGround");
        getLayoutInflater().setFactory(new Factory() {
    
            public View onCreateView(String name, Context context,
                    AttributeSet attrs) {
    
                if (name
                        .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
    
                    try { // Ask our inflater to create the view
                        LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);
    
                        /*
                         * The background gets refreshed each time a new item is
                         * added the options menu. So each time Android applies
                         * the default background we need to set our own
                         * background. This is done using a thread giving the
                         * background change as runnable object
                         */
                        new Handler().post(new Runnable() {
                            public void run() {
    
                                view
                                        .setBackgroundResource(R.drawable.row_blue_menu);
                            }
                        });
                        return view;
                    } catch (InflateException e) {
                    } catch (ClassNotFoundException e) {
                    }
                }
                return null;
            }
        });
    }
    

    .

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.start_menue, menu);
        setMenuBackground();
        return true;
    }
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
提交回复
热议问题