【问题标题】:AppCompat ToolBar popupTheme not used in the ShareAction MenuItemAppCompat ToolBar popupTheme 未在 ShareAction MenuItem 中使用
【发布时间】:2014-12-17 17:32:01
【问题描述】:

上下文

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

溢出菜单项正在正确使用popupTheme

另一方面,ShareAction 不接收 popupTheme。经过一些测试后,我注意到它收到了工具栏的 app:theme,因此是黑暗的。

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

为了在 ShareAction 上获得黑色文本,我尝试设置许多属性并通过设置“android:textColorPrimary”(在 ToolBar 主题上)我得到了我想要的但我在 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 和溢出都有 popupTheme 但事实并非如此

解决方法

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

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

【问题讨论】:

    标签: android styles themes android-appcompat android-toolbar


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

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

      请注意,我同时设置了主题和 popupTheme,并且我还将背景覆盖为 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>
      

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

      【讨论】:

      • 添加一些有用的东西是 true,对我来说,选择器仅在按其他方式时显示为列表中单词的边框
      • @Max 是的,它没有。 v7:23.1.1 的新解决方案是什么
      【解决方案3】:

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

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

      【讨论】:

      • 是的,android/cycle 是复制粘贴错误。并且该网站没有谈论这个特定问题,它实际上是 AppCompat 库中的一个错误,将很快修复
      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 2015-08-26
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多