在android中更改菜单选项的背景对我来说没有任何效果

在android中更改菜单选项的背景对我来说没有任何效果,android,menu,background-color,Android,Menu,Background Color,我尝试了几种技术来更改菜单选项的背景(默认为黑色,我必须更改),但没有任何效果。我使用的是min sdk 11 我使用了两种方法: 第一)更改OnCreateOptions菜单功能: @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); MenuInflater inflate

我尝试了几种技术来更改菜单选项的背景(默认为黑色,我必须更改),但没有任何效果。我使用的是min sdk 11

我使用了两种方法:

第一)更改OnCreateOptions菜单功能:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = new MenuInflater(getApplicationContext());
    inflater.inflate(R.menu.options_menu, menu);
    setMenuBackground();
    return true;
}

/*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(){
    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 () {
                            view.setBackgroundResource( R.drawable.my_gradient);
                        }
                    } );
                    return view;
                }
                catch ( InflateException e ) {}
                catch ( ClassNotFoundException e ) {}
            }
            return null;
        }
    });
}
}

第二)在styles.xml中更改

<style name="Theme_MyStyled" parent="AppBaseTheme">
   <item name="android:panelFullBackground">@drawable/my_gradient</item>
</style>

@可牵引/我的车坡度
我的提款权是:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
  <gradient android:startColor="#990000ff" android:centerColor="#99800000" android:endColor="#99FFD485" android:angle="270" />
</shape>


有人能告诉我这怎么行吗?提前感谢。

设置自定义主题的第二个选项应该适合您。您是否正在将此主题设置为选项菜单。我建议您参考,甚至可能会有所帮助you@Sushil:是的。。OptionsMenu@Ritesh:我已经通过了链接bro,我的两次尝试就是从那里得到的结果。。