Android: customize application's menu (e.g bac

2019-01-02 15:53发布

What is the way (if there is a way) to customize the menu (the one triggered by the MENU button of the phone). I'm especially interested in two things:

  • changing the background color from the standard light gray into a darker gray
  • how the menu items are aligned. I have 4 items and they are automatically aligned 2x2, but I would prefer to have them all in one line (1x4)

6条回答
栀子花@的思念
2楼-- · 2019-01-02 16:22

background menu color in style.xml in your theme

<item name="android:panelFullBackground">@android:color/darker_gray</item>
查看更多
孤独总比滥情好
3楼-- · 2019-01-02 16:23

You can also just implement the "onCreateOptionsMenu" method, that is usually used to display the standard menu, and display whatever you want in this case.

In my game, I implemented it to display a "Game Paused" dialog box when the menu button is pressed...

查看更多
还给你的自由
4楼-- · 2019-01-02 16:24

I created my own menu class. It maybe isn't exactly what you want but it should hopefully get you started. Here is the article I published and a downloadable link to the source code.

http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx

查看更多
与君花间醉酒
5楼-- · 2019-01-02 16:34

This answer works but crashed for me when using ActionBarSherlock. Here's a hacky workaround to make this work nontheless.

    // Black Vodoo! Do not try this at home.

    final LayoutInflater li = getLayoutInflater();

    final Class<LayoutInflater> clazz = LayoutInflater.class;

    try {
        final Field fieldSet = clazz.getDeclaredField("mFactorySet");
        fieldSet.setAccessible(true);
        fieldSet.setBoolean(li, false);

        li.setFactory(new Factory() {

            @Override
            public View onCreateView(final String name,
                    final Context context, final AttributeSet attrs) {
                if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                    try {
                        final LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                // Set the text color
                                ((TextView) view).setTextColor(Color.WHITE);
                            }
                        });
                        return view;
                    } catch (final Exception e) {
                    }
                }
                return null;
            }
        });
    } catch (final Exception e) {
        e.printStackTrace();
    }
查看更多
还给你的自由
6楼-- · 2019-01-02 16:38

Use styles. This works for me on Android 5.0

<style name="AppTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:actionOverflowMenuStyle">@style/PopupMenu.MyStyle</item>
</style>

<style name="PopupMenu.MyStyle" parent="android:Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/actionbar_item_background</item>
</style>

... then the drawable is a regular selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/primary"/>
    <item android:drawable="@color/highlighted" android:state_pressed="true"/>
</selector>
查看更多
浪荡孟婆
7楼-- · 2019-01-02 16:43

Not with the built-in menu framework.

You are welcome to intercept the MENU button (via onKeyDown() or something) and render what you want, but bear in mind that users will be expecting it to look like the rest of the menus do on their device.

查看更多
登录 后发表回答