AppCompat ToolBar popupTheme 未在 ShareAction MenuItem 中使用 [英] AppCompat ToolBar popupTheme not used in the ShareAction MenuItem
问题描述
背景
使用 AppCompat v7 21.0.0/21.0.2/21.0.3
Using the AppCompat v7 21.0.0 / 21.0.2 / 21.0.3
问题
ToolBar 的 popupTheme 不适用于 ShareAction
The popupTheme of the ToolBar is not applied to the 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>
溢出菜单项正确使用了popupTheme
The overflow menu item is using the popupTheme properly
另一方面,ShareAction 没有收到 popupTheme.经过一些测试,我注意到它收到了 app:theme of the ToolBar 因此变暗了.
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.
<item name="android:colorBackground">@color/white</item>
为了在 ShareAction 上获得黑色文本,我尝试设置许多属性,并通过设置android:textColorPrimary"(在 ToolBar 主题上)我得到了我想要的但是我在 ToolBar 上的图标也采用了这种颜色奇怪...
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...
菜单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 和溢出有 popupTheme 但事实并非如此
I would expect both the ShareAction & the overflow to have the popupTheme but it's not the case
解决方法
我会在找到解决方法后编辑这篇文章
I'll edit this post once I got a workaround
参考:https://code.google.com/p/android/issues/detail?id=87285&thanks=87285&ts=1419254842
推荐答案
所以,这对我有用.这是我的工具栏 xml:
So, here's what worked for me. Here's my Toolbar 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.以下是工具栏主题的主要应用主题说明:
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:
<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.
这篇关于AppCompat ToolBar popupTheme 未在 ShareAction MenuItem 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!