简体   繁体   English

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

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

What is the way (if there is a way) to customize the menu (the one triggered by the MENU button of the phone). 有什么方法(如果有办法)自定义菜单(由手机的MENU按钮触发的菜单)。 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) 我有4个项目,它们自动对齐2x2,但我更喜欢将它们全部放在一行(1x4)

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

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()或其他东西)并渲染您想要的内容,但请记住,用户希望它看起来像其他菜单在他们的设备上。

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... 在我的游戏中,当按下菜单按钮时,我实现了它以显示“Game Paused”对话框...

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>

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

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

This answer works but crashed for me when using ActionBarSherlock. 这个答案有效但在使用ActionBarSherlock时崩溃了。 Here's a hacky workaround to make this work nontheless. 尽管如此,这仍然是一个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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何通过长时间运行的后台服务触发屏幕UI事件(例如,来电屏幕)? - Android: How to trigger screen UI events (e.g incoming call screen) by a long running background service? Xamarin形式:如何找出android设备的大小桶,例如小号,中号等 - Xamarin form: How to find out the android device's size bucket e.g small, medium etc 如何自定义菜单项的背景色? - How do I customize menu item's background color? 在Android Studio与Framework中进行开发(例如PhoneGap) - Developing in Android Studio versus Framework (e.g PhoneGap) ANDROID:Gradle传入参数,例如serverURL - ANDROID : Gradle passing in a parameter e.g serverURL 如何自定义状态栏图标和文字颜色? 例如状态栏背景:白色,状态栏图标颜色,文字:红色 - How to customize status bar icons and text color? E.g. status bar background: white, status bar icon color, and text: red Android的TabLayout,自定义背景色 - TabLayout Android, customize background color 是否可以通过背景颜色和填充来自定义菜单项? - Is it possible to customize the menu items by background color and padding? 如何将存储在本地存储中的文件(例如应用程序数据库文件夹)附加到gmail - How to attach files stored in local store e.g application database folder to gmail 如何更好地控制 Android SAF UI(例如 ACTION_OPEN_DOCUMENT)? - How to get more control over Android SAF UI (e.g ACTION_OPEN_DOCUMENT)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM