Android:自定义应用程序的菜单(例如背景颜色)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1593529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:19:34  来源:igfitidea点击:

Android: customize application's menu (e.g background color)

androidcustomizationandroid-menu

提问by znq

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:

有什么办法(如果有办法)自定义菜单(通过手机的MENU键触发的那个)。我对两件事特别感兴趣:

  • 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)
  • 将背景颜色从标准浅灰色更改为深灰色
  • 菜单项如何对齐。我有 4 个项目,它们自动对齐 2x2,但我更愿意将它们全部放在一行 (1x4)

采纳答案by CommonsWare

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.

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

回答by authorwjf

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

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

回答by alocaly

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.

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

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

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

回答by Marco Bonechi

Use styles. This works for me on Android 5.0

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

...那么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>

回答by wdog

background menu color in style.xml in your theme

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

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

回答by Kirill Rakhman

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

这个答案有效,但在使用 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();
    }