开发者

White background on optionsmenu (Android)

In my Samsung Galaxy when I press the menu button the menu have a very dark blue (almost black) background. I think that background color is rom/version theme dependant by default. For my app I need a white (or very light) background, else my icons are practically invisible. But I don't see any way of changin the menu's background.

开发者_如何转开发

How can I reach that?

Thanks in advance


Put this in your activity class:

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
                    {
                            LayoutInflater f = getLayoutInflater();
                            final View view = f.createView( name, null, attrs );
                            new Handler().post( new Runnable()
                            {
                                public void run()
                                {
                                    // Changes the color of the menu item here
                                    view.setBackgroundColor(Color.WHITE);
                                }
                            });
                            return view;
                    }
                    catch (InflateException e)
                    {
                    }
                    catch (ClassNotFoundException e )
                    {
                    }
            }
            return null;
        }
    });
}

Then create the onCreateOptionsMenu() listener:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    menu.clear();
    // Add menus here
    setMenuBackground();
    return true;
}

This altogether will allow you change the background of the options menu to any color or image (in this case white).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜