AppCompat工具栏中的popupTheme在ShareAction MenuItem中未使用。

29

背景

使用AppCompat v7 21.0.0 / 21.0.2 / 21.0.3应用程序兼容库。

问题

ToolBar的popupTheme未应用于ShareAction。

工具栏上的样式:

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

溢出菜单项已经正确地使用了弹出主题。

溢出菜单项

另一方面,分享操作没有接收到弹出主题。通过一些测试,我注意到它接收到了工具栏的 app:theme 主题,因此是黑色的。

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

分享操作菜单项

为了在“分享操作”上获得黑色文字,我尝试设置许多属性,并通过设置“android:textColorPrimary”(在ToolBar主题上)获得了我想要的效果,但是我的工具栏上的图标也会变成这种颜色,这很奇怪...

以下是菜单的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>

我希望ShareAction和overflow都有popupTheme,但事实并非如此。

解决方法

一旦我找到了解决方法,我将编辑此帖子。

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

3个回答

30

所以,这是对我有用的。 这是我的工具栏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" />

注意我设置了主题和弹出主题,我还覆盖了背景颜色为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>

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


1
有一个有用的补充是<item name="android:drawSelectorOnTop">true</item>,对我来说,在列表中按下时选择器只显示为单词周围的边框。 - Daniel Wilson
@Max,是的,它不行。v7:23.1.1有什么新的解决方案? - Edijae Crusar

1

我一直在苦苦思索如何解决这个问题。最终,我找到了解决方案。希望这也能帮助其他人:

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

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

在我的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>

我希望我的工具栏中的操作图标是白色的,因此我将这些值设置为白色。不幸的是,这也使得我的ShareActionProvider菜单文本变成了白色(在白色背景上)。
对我来说,解决方案是删除textColorPrimary的样式设置。我的工具栏图标仍然是白色的,但现在我在弹出的共享提供程序菜单中有所需的深色文本。
<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorControlNormal">#fff</item>
    <item name="colorControlHighlight">#3fff</item>
</style>

0

1
是的,android/cycle 是一个复制粘贴错误。网站并没有提到这个特定的问题,实际上这是一个 AppCompat 库中的 bug,很快就会被修复。 - Benoit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接