如何更改操作栏的选项菜单的背景颜色的Andr​​oid 4.2?选项、如何更改、菜单、颜色

2023-09-12 21:31:15 作者:孤城姐坐宝马

我想更改的选项(溢)菜单的背景颜色的Andr​​oid 4.2。我已经尝试了所有方法,但它仍然显示的默认颜色的主题设置。我用下面的code和; XML的configs。

I'd like to change the background color of the option (overflow) menu in Android 4.2. I have tried all the methods but it is still showing the default color set by the theme. I used the following code & XML configs.

MainActivity.java

public class MainActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getActionBar().setIcon(R.drawable.ic_launcher);     
    getActionBar().setTitle("Sample Menu");
    getActionBar().setBackgroundDrawable(new 
               ColorDrawable(Color.parseColor("#33B5E5"))); 

    int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    TextView titleText = (TextView)findViewById(titleId);
    titleText.setTextColor(Color.parseColor("#ffffff"));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    setMenuBackground();
    return true;
}

protected void setMenuBackground(){                     
    // Log.d(TAG, "Enterting setMenuBackGround");  
    getLayoutInflater().setFactory( new Factory() { 

        @Override
        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.menubg);
                            // sets the text color              
                            ((TextView) view).setTextColor(Color.WHITE);
                            // sets the text size              
                            ((TextView) view).setTextSize(18);
            }
                    } );  
                return view;
            }
        catch ( InflateException e ) {}
        catch ( ClassNotFoundException e ) {}  
    } 
            return null;
        }});
}

}

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/menu"
    android:showAsAction="always"
    android:title="@string/action_settings">
    <menu>
        <item
            android:id="@+id/item1"             
            android:showAsAction="always"
            android:title="@string/item1" />
        <item
            android:id="@+id/item2"             
            android:showAsAction="always"
            android:title="@string/item2" />
        <item
            android:id="@+id/item3"
            android:showAsAction="always"
            android:title="@string/item3" />
        <item
            android:id="@+id/item4"
            android:showAsAction="always"
            android:title="@string/item4" />
    </menu>
</item>

</menu>

color.xml

<color name="menubg">#33B5E5</color>

以上setMenuBackground没有采取任何影响:

在上面的图片,我想从黑色的菜单背景更改为蓝色,在操作栏。我怎样才能做到这一点,我没有做错了什么?

In the above picture, I want to change the menu background from black to the blue color in the Action Bar. How can I achieve this, and what I did do wrong?

推荐答案

有一个简单的方法来改变动作条的颜色 使用动作条发生器并复制粘贴的所有文件中的 RES 文件夹和更改Android.manifest文件中的主题。

There is an easy way to change the colors in Actionbar Use ActionBar Generator and copy paste all file in your res folder and change your theme in Android.manifest file.