如何为动作项目(包括溢出菜单)的弹出窗口设置自定义颜色? [英] How to set a custom color for the popup of action items, including of the overflow menu?

查看:148
本文介绍了如何为动作项目(包括溢出菜单)的弹出窗口设置自定义颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为应用程序添加一些材料设计样式,因此我为操作栏和状态栏选择了不同的颜色.

I'm working on adding some material design style for an app, so I've chosen a different color for the action bar and status bar.

为此,该应用程序的主题为"Theme.AppCompat.Light.DarkActionBar",并添加了此按钮以隐藏操作栏,因为我需要将其作为工具栏进行处理:

For this, the theme of the app is of "Theme.AppCompat.Light.DarkActionBar", and added this to hide the action bar as I need to handle it as a toolbar:

我使用过的主题:

<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);
    }

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

and for some reason I get this behavior:

  1. 操作项和溢出项的子菜单为黑色.
  2. 单击搜索操作项时,其中的溢出项为白色.

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

I'd like to customize those popup menus so that they will be consistent.

我尝试使用此功能:

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

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

但这完全没有帮助.

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

Does anyone know how to handle this? Is this a bug in the support library?

对我来说,它看起来是这样的,所以我已经举报了 此处 ,其中包括一个示例项目.

For me it looks this way, so I've reported about it here, including a sample project.

推荐答案

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

You can customize overflow menu with the popupTheme attribute:

<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" />


原始答案缺少一些要点:


Original answer was missing some points:

首先,工具栏应具有:

  <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"/>

对于弹出式窗口,请使用此:

For light popup, use this:

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

对于深色弹出窗口,请使用此:

For dark popup, use this:

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

这篇关于如何为动作项目(包括溢出菜单)的弹出窗口设置自定义颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆