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

  • 将背景颜色从标准浅灰色更改为深灰色
  • 菜单项如何对齐。我有 4 个项目,它们会自动对齐 2x2,但我更喜欢将它们全部排成一行 (1x4)
有帮助吗?

解决方案

不适用于内置菜单框架。

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

其他提示

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

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

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

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

使用样式。这对我的 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>

...那么可绘制对象是一个常规选择器

<?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>

样式 .xml 中的背景菜单颜色 在您的主题中

<item name="android:panelFullBackground">@android:color/darker_gray</item>

这个答案 有效,但在使用 ActionBarSherlock 时对我来说崩溃了。尽管如此,这里有一个巧妙的解决方法可以使这项工作正常进行。

    // 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();
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top