【问题标题】:Android: customize application's menu (e.g background color)Android:自定义应用程序的菜单(例如背景颜色)
【发布时间】:2010-12-08 06:56:46
【问题描述】:

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

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

【问题讨论】:

  • 现在,这个问题已经过时了,但是对于那些希望自定义 Action Bar overflow 菜单背景颜色的人(例如,在 Android 4.0+ 上),请参阅以下解决方案:stackoverflow.com/a/20077381/56285

标签: android customization android-menu


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    不适用于内置菜单框架。

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

    【讨论】:

    • 谢谢。然后我暂时跳过这个,也许在以后有更多时间的时候实现它。
    • 我已经看到了菜单的自定义排列(如此处所示androidandme.com/wp-content/uploads/2009/12/…),所以一定有办法做到这一点。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案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>
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          This answer 可以工作,但在使用 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();
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-05-06
            • 1970-01-01
            • 2012-07-14
            • 2017-08-01
            • 2011-09-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多