Android的菜单更改背景颜色菜单、颜色、背景、Android

2023-09-12 00:36:18 作者:想了太多心会痛

我想标准的浅灰色变为浅绿色。看来,没有一个简单的方法来做到这一点(通过Android的主题,例如),但我已经找到了解决方法,因为在此页解释: HTTP:/ /tinyurl.com/342dgn3 。

I'm trying to change the standard light grey to a light green. Seems that there is not a simple way to do this (through Android Themes, for example) but I have found a workaround as explained at this page: http://tinyurl.com/342dgn3.

在笔者看来消失了,有人可以帮我这个整合code?我不明白,我需要落实 LayoutInflater 工厂类。

The author seems disappeared, can someone help me integrating this code? I don't understand where I need to implement the LayoutInflater factory class.

推荐答案

在乌拉圭回合被夸大菜单调用这个setMenuBackground()方法

When ur are inflating the menu call this setMenuBackground() method

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu,menu);
    setMenuBackground(); 
    return true;    
}

和setMenuBackground()方法写这个

and write this in the setMenuBackground() method

    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 () {  
                                // sets the background color   
                                view.setBackgroundResource( R.color.androidcolor);
                                // sets the text color              
                                ((TextView) view).setTextColor(Color.BLACK);
                                // sets the text size              
                                ((TextView) view).setTextSize(18);
                }
                        } );  
                    return view;
                }
            catch ( InflateException e ) {}
            catch ( ClassNotFoundException e ) {}  
        } 
        return null;
    }}); 
}
 
精彩推荐