How to set a custom color for the popup of action items, including of the overflow menu?

∥☆過路亽.° 提交于 2019-12-10 09:58:59

问题


Background

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.

The problem

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:

theme I've used:

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

layout I've used :

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

code:

  @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. sub menus of action items and of the overflow item are black.
  2. when clicking the search action item , the overflow item there is white.

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

What I've tried

I've tried using this:

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

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

But it didn't help at all.

The question

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.


回答1:


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:

First, the toolbar should have:

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


来源:https://stackoverflow.com/questions/30148070/how-to-set-a-custom-color-for-the-popup-of-action-items-including-of-the-overfl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!