【问题标题】:How to set a custom color for the popup of action items, including of the overflow menu?如何为操作项的弹出设置自定义颜色,包括溢出菜单?
【发布时间】:2015-05-10 05:30:41
【问题描述】:

背景

我正在为应用添加一些材料设计风格,因此我为操作栏和状态栏选择了不同的颜色。

问题

为此,应用程序的主题是“Theme.AppCompat.Light.DarkActionBar”,并添加了这个来隐藏操作栏,因为我需要将其作为工具栏处理:

我使用过的主题:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionModeOverlay">true</item>
</style>

我使用的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_app_list__toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        tools:ignore="UnusedAttribute"/>
</LinearLayout>

代码:

  @Override
  protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _toolbar=(Toolbar)findViewById(R.id.activity_app_list__toolbar);
    setSupportActionBar(_toolbar);
    }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
    {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
    }

由于某种原因,我得到了这种行为:

  1. 操作项和溢出项的子菜单为黑色。
  2. 点击搜索动作项时,溢出的项是白色的。

我想自定义这些弹出菜单,以便它们保持一致。

我尝试过的

我试过用这个:

<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>

...
<style name="OverflowMenu" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
</style>

但它根本没有帮助。

问题

有人知道如何处理吗?这是支持库中的错误吗?

对我来说它看起来是这样的,所以我已经报告了它here,包括一个示例项目。

【问题讨论】:

  • 你的意思是你想要一个 Light Overflow 菜单,而其他人都在 Dark 中?

标签: android android-actionbar android-support-library popupmenu overflow-menu


【解决方案1】:

您可以使用popupTheme 属性自定义溢出菜单:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="@dimen/triple_height_toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

原始答案缺少一些要点:

首先,工具栏应该有:

  <android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"/>

对于灯光弹出,使用这个:

  <style name="AppTheme.Light" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarTheme">@style/AppTheme.ActionBarTheme.Light</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
  </style>

  <style name="AppTheme.ActionBarTheme.Light" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlActivated">#FFffffff</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
  </style>

对于黑暗的弹出窗口,使用这个:

  <style name="AppTheme.Light" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Dark</item>
    <item name="actionBarTheme">@style/AppTheme.ActionBarTheme.Dark</item>
  </style>

  <style name="AppTheme.ActionBarTheme.Dark" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlActivated">#FFffffff</item>
    <item name="android:textColorPrimary">#FFffffff</item>
  </style>

【讨论】:

  • 这个有效。关于这种不一致行为的原因的任何想法?另外,假设我希望它们都是黑色的,应该怎么做?它也适用于子菜单吗?
  • 新工具栏使用app:popupTheme样式溢出菜单,没有不一致。如果你想要黑色溢出菜单,你应该使用@style/ThemeOverlay.AppCompat。它还应该设置子菜单的样式。
  • 使用“@style/ThemeOverlay.AppCompat”不起作用,因为它在使用 actionMode 时不会影响溢出菜单(对我来说,在这种情况下它仍然是白色的)。这就是我称之为不一致的原因,因为一个溢出菜单是暗的,另一个是亮的。
  • 你可以尝试使用@style/ThemeOverlay.AppCompat.Dark
  • 搜索操作项溢出仍为黑色,actionMode 为白色。我也想知道如何自己改变颜色。我什至找不到如何更改搜索项的文本插入符号的颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多