Change menu background color on android 2.3

2019-06-14 04:59发布

How could I change menu background color?

I try this but it is not work for me:

<style name="MyTheme" parent="@android:style/Theme" >
    <item name="android:itemBackground">@drawable/menuitem_background</item>
</style>

Could you help me?

Also I use this :

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 );
                    // Kind of apply our own background
                    new Handler().post( new Runnable() {
                        public void run () {
                            view.setBackgroundResource( R.drawable.background);
                        }
                    } );
                    return view;
                }
                catch ( InflateException e ) {
                    e.printStackTrace();
                }
                catch ( ClassNotFoundException e ) {
                    e.printStackTrace();
                }
            }
            return null;
        }


    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.option_menu, menu);

    setMenuBackground();
    return true;

}

But I have the same result Nothing change

1条回答
▲ chillily
2楼-- · 2019-06-14 05:19

Use setMenuBackground ........

查看更多
登录 后发表回答