Android AppCompat ToolBar popupTheme 未在 ShareAction MenuItem 中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26504532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:37:40  来源:igfitidea点击:

AppCompat ToolBar popupTheme not used in the ShareAction MenuItem

androidstylesthemesandroid-appcompatandroid-toolbar

提问by Benoit

Context

语境

Using the AppCompat v7 21.0.0 / 21.0.2 / 21.0.3

使用 AppCompat v7 21.0.0 / 21.0.2 / 21.0.3

Problem

问题

The popupTheme of the ToolBar is not applied to the ShareAction

ToolBar 的 popupTheme 不适用于 ShareAction

Style on the toolbar:

工具栏上的样式:

<style name="MyActionBarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/green</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

The overflow menu item is using the popupTheme properly

溢出菜单项正确使用 popupTheme

Overflow Item

溢出项

The ShareAction on the other hand does not receive the popupTheme. After some testing I noticed it received the app:theme of the ToolBar thus being dark.

另一方面,ShareAction 不会收到 popupTheme。经过一些测试,我注意到它收到了 app:theme of the ToolBar 因此是黑暗的。

<item name="android:colorBackground">@color/white</item>

ShareAction Menu Item

ShareAction 菜单项

In order to get the black text on the ShareAction I tried setting many attributes and by setting "android:textColorPrimary" (on the ToolBar theme) I get what I want BUT then my icons on the ToolBar also takes this color which is weird...

为了在 ShareAction 上获得黑色文本,我尝试设置许多属性,并通过设置“android:textColorPrimary”(在 ToolBar 主题上)我得到了我想要的但是我在 ToolBar 上的图标也采用了这种奇怪的颜色.. .

The menu xml is the following:

菜单xml如下:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cycle="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/ic_share"
        android:icon="@drawable/abc_ic_menu_share_holo_dark"
        android:title="@string/media_share"
        cycle:showAsAction="ifRoom"
        cycle:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
    <item
        android:icon="@drawable/abc_ic_menu_share_holo_dark"
        android:showAsAction="ifRoom"
        android:title="br">
        <menu>
            <item
                android:id="@+id/menuSortNewest"
                android:title="Sort by newest" />
            <item
                android:id="@+id/menuSortRating"
                android:title="Sort by rating" />
        </menu>
    </item>

</menu>

I would expect both the ShareAction & the overflow to have the popupTheme but it's not the case

我希望 ShareAction 和溢出都有 popupTheme 但事实并非如此

Workaround

解决方法

I'll edit this post once I got a workaround

找到解决方法后,我会编辑这篇文章

Ref: https://code.google.com/p/android/issues/detail?id=87285&thanks=87285&ts=1419254842

参考:https: //code.google.com/p/android/issues/detail?id =87285 &thanks =87285 &ts =1419254842

采纳答案by AAverin

So, here's what worked for me. Here's my Toolbar xml:

所以,这对我有用。这是我的工具栏 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/action_bar_main"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    app:theme="@style/Toolbar"
    app:popupTheme="@style/Toolbar_Popup"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

Notice that I set both theme and popupTheme, and I also override background to be colorPrimary. Here's the main app theme description with themes for Toolbar:

请注意,我同时设置了 theme 和 popupTheme,并且还覆盖了 background 为 colorPrimary。以下是工具栏主题的主要应用程序主题说明:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionMenuTextColor">@color/white</item>

    <!-- Support library compatibility -->
    <item name="actionMenuTextColor">@color/white</item>

    <item name="actionBarSize">@dimen/actionbar_height</item>

    <item name="colorPrimary">@color/dark_blue</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="android:textColorPrimary">#607d8b</item>

</style>

<style name="Toolbar" parent="Base.ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">#fff</item>
    <item name="android:background">@color/dark_blue</item>
</style>

<style name="Toolbar_Popup" parent="Base.ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">#fff</item>
    <item name="android:background">@color/dark_blue</item>
</style>

So, as a result, Share action background is set to the value of background in the main Toolbar theme. And the background of Toolbar itself is overriden.

因此,因此,共享操作背景设置为主工具栏主题中的背景值。并且工具栏本身的背景被覆盖。

回答by Paul LeBeau

I was scratching my head for ages trying to solve this. I found a solution to my problem. Hopefuully this will help others also:

我一直在挠头试图解决这个问题。我找到了解决我的问题的方法。希望这也能帮助其他人:

In my toolbar definition I was setting a custom theme like so:

在我的工具栏定义中,我设置了一个自定义主题,如下所示:

<android.support.v7.widget.Toolbar     
    ...
    app:theme="@style/ActionBarThemeOverlay"
    ... />

In my styles.xml, my theme was defined as:

在我的styles.xml 中,我的主题被定义为:

<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorPrimary">#fff</item>
    <item name="colorControlNormal">#fff</item>
    <item name="colorControlHighlight">#3fff</item>
</style>

I wanted white action icons in my toolbar, so I was setting these values to white. Unfortunately it was also making my ShareActionProvider menu text white (on a white background).

我想要工具栏中的白色动作图标,所以我将这些值设置为白色。不幸的是,它也使我的 ShareActionProvider 菜单文本变为白色(在白色背景上)。

The solution for me was to remove the style setting for textColorPrimary. My toolbar icons were still white but I now had the desired dark text in the popup share provider menu.

我的解决方案是删除textColorPrimary. 我的工具栏图标仍然是白色的,但我现在在弹出的共享提供程序菜单中有所需的深色文本。

<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorControlNormal">#fff</item>
    <item name="colorControlHighlight">#3fff</item>
</style>

回答by Lukas

This page may helps to solve your problem: http://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/

此页面可能有助于解决您的问题:http: //www.murrayc.com/permalink/2014/10/28/android-changed-the-toolbars-text-color-and-overflow-icon-color/

One difference I noticed in your XML is that you once use the property android:showAsAction="ifRoom" and once cycle:showAsAction="ifRoom".

我在您的 XML 中注意到的一个区别是,您曾经使用过属性 android:showAsAction="ifRoom" 和一次 cycle:showAsAction="ifRoom"。