19

自定义菜单(由手机的MENU按钮触发的那个)是什么方式(如果有办法)。我对两件事特别感兴趣:

  • 将背景颜色从标准浅灰色更改为深灰色
  • 菜单项如何对齐。我有 4 个项目,它们自动对齐 2x2,但我希望将它们全部放在一行 (1x4)
4

6 回答 6

13

我创建了自己的菜单类。它可能不是您想要的,但希望它可以帮助您入门。这是我发表的文章和源代码的可下载链接。

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

于 2011-03-29T16:22:20.270 回答
8

不使用内置菜单框架。

欢迎您拦截菜单按钮(通过onKeyDown()或其他方式)并呈现您想要的内容,但请记住,用户会期望它看起来像他们设备上的其他菜单一样。

于 2009-10-20T18:26:21.807 回答
4

您也可以只实现“onCreateOptionsMenu”方法,该方法通常用于显示标准菜单,并在这种情况下显示您想要的任何内容。

在我的游戏中,我实现它以在按下菜单按钮时显示“游戏暂停”对话框......

于 2009-10-20T23:59:53.967 回答
4

使用样式。这适用于我在 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>

...然后drawable是一个常规选择器

<?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>
于 2014-12-09T02:05:37.203 回答
2

主题中 style.xml 中的背景菜单颜色

<item name="android:panelFullBackground">@android:color/darker_gray</item>
于 2013-04-07T20:23:37.190 回答
0

这个答案有效,但在使用 ActionBarSherlock 时对我来说崩溃了。尽管如此,这里有一个 hacky 解决方法来完成这项工作。

    // 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();
    }
于 2013-08-30T14:11:21.967 回答