【问题标题】:How to specify dark action mode with my theme如何使用我的主题指定暗动作模式
【发布时间】:2015-03-22 23:30:24
【问题描述】:

我知道有几个关于操作栏的上下文操作栏 (ActionMode) 部分的样式的问题,但它们似乎并没有完全解决我所追求的问题。

我正在使用带有浅色主题和深色操作栏的工具栏。工具栏看起来像我想要的,但动作模式看起来像常规的深色主题。我需要改变我的风格以获得黑暗主题的动作模式(不仅仅是动作栏)?看来我应该能够通过点击Theme.AppCompat 快​​速做到这一点,因为这向 CAB 显示了我想要的方式,但我不希望应用程序的其余部分变暗。

我只关心 API 14+,我使用支持工具栏代替操作栏。

这是我的基本风格

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:actionModeBackground">@color/colorActionMode</item>
    <item name="android:windowActionModeOverlay">true</item>
</style>

工具栏样式

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item>
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
    <item name="android:textColorSecondary">@color/abc_primary_text_material_dark</item>
    <item name="android:background">@color/colorPrimaryDark</item>
</style>

工具栏布局文件(这里设置popupTheme好像没有任何效果)。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    app:theme="@style/AppTheme.Toolbar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="2dp"
    android:focusable="false"/>

这是我的工具栏(这是我想要的)

这是我的 ActionMode(我需要反转)

这就是我希望 ActionMode 看起来的样子(我通过将样式更改为从 Theme.AppCompat 继承而不是 Theme.AppCompat.Light.DarkActionBar 得到。问题是应用程序的其余部分变暗了,这是我不想要的.

【问题讨论】:

    标签: android android-actionbar android-toolbar android-actionmode


    【解决方案1】:

    查看这篇博文,其中解释了如何将工具栏设置为深色:

    http://android-developers.blogspot.ie/2014/10/appcompat-v21-material-design-for-pre.html

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

    您指定工具栏“样式”。我认为您缺少的是应用程序命名空间的“主题”属性,该属性将样式应用于工具栏及其所有子项。

    编辑:这应该适用于您的具体情况

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:actionModeBackground">@color/colorActionMode</item>
        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
    <style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item>
        <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
        <item name="android:textColorSecondary">@color/abc_primary_text_material_dark</item>
        <item name="android:background">@color/colorPrimaryDark</item>
    </style>
    

    【讨论】:

    • 我已经为工具栏设置了主题,我会将工具栏声明添加到问题中。设置 popupTheme 似乎没有任何效果。我试过 app.popupTheme 以及 android:popupTheme。
    • 我有相同的设置,我的应用程序主题继承自 Theme.AppCompat.Light.NoActionBar,我的工具栏主题继承自 ThemeOverlay.AppCompat.Dark.ActionBar,如我的答案中所指定。
    • 不幸的是,这些风格与我已经拥有的风格基本相同。 Theme.AppCompat.Light.NoActionBar 只添加了我在样式中已经拥有的东西,而工具栏的 Dark.ActionBar 主题并没有改变任何东西。当我尝试你那里的东西时,我得到了与最初相同的结果。
    • 是的,我明白你对NoActionBar 的看法,但ThemeOverlay.AppCompat.Dark.ActionBar 确实应该是这里的关键。只是检查一下你注意到它以ThemeOverlay 开头,而不仅仅是Theme 对吗?哈
    • 是的,我复制并粘贴了您那里的内容。在我看来,ActionMode 部分和 Toolbar 是完全分开的。由于ThemeOverlay.AppCompat.Dark.ActionBar,工具栏看起来很棒,但是当它切换到 ActionMode 时,它​​似乎并没有延续。
    【解决方案2】:

    从基础主题中的覆盖属性 actionModeStyle 开始:

    <item name="actionModeStyle">@style/LStyled.ActionMode</item>
    

    LStyled.ActionMode 定义为:

    <style name="LStyled.ActionMode" parent="@style/Widget.AppCompat.ActionMode">
        <item name="titleTextStyle">@style/LStyled.ActionMode.Title</item>
        <item name="subtitleTextStyle">@style/LStyled.ActionMode.Subtitle</item>
    </style>
    

    最后:

    <style name="LStyled.ActionMode.Title" parent="@style/TextAppearance.AppCompat.Widget.ActionMode.Title">
        <item name="android:textColor">@color/color_action_mode_text</item>
    </style>
    
    <style name="LStyled.ActionMode.Subtitle" parent="@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle">
        <item name="android:textColor">@color/color_action_mode_text</item>
    </style>
    

    这将覆盖标题和副标题的主题指定文本颜色(如果需要)。

    剩下的是溢出按钮。启动任何支持颜色替换的图像编辑软件。使用 AppCompat 的 res/drawable-XXXX 文件夹中的溢出菜单 png。把它涂成白色。接下来,在您的基本主题中覆盖 actionOverflowButtonStyle 并指定您刚刚修改的 png:

    <item name="actionOverflowButtonStyle">@style/LStyled.OverFlow</item>
    
    <style name="LStyled.OverFlow" parent="@style/Widget.AppCompat.ActionButton.Overflow">
        <item name="android:src">@drawable/modified_overflow_icon</item>
    </style>
    

    关于主题的自定义,我没有太多可以提供的解释/教程。掌握它的唯一方法是浏览框架中的 [styles][themes].xml 文件。阅读源代码也有帮助 - 您将了解正在使用哪些属性以及在哪里/是否可以自定义它们。

    提点:

    我希望能够扩展一种样式以获得相同的行为 内置于深色主题中。

    是的,但这可能不是我们想要的。上面处理修改溢出菜单图标的部分可以替换为覆盖的colorControlNormal 属性。 TintManager (Link) 使用来自colorControlNormal 的颜色值对溢出菜单可绘制对象(以及其他可绘制对象)进行着色。看看源码中数组TINT_COLOR_CONTROL_NORMAL的内容。但是覆盖colorControlNormal 来修复一个drawable 可能会使应用程序的整体外观和感觉变得更糟。

    【讨论】:

    • 感谢您的回答,但我正在寻找一种方法来扩展内置样式。我知道我可以在动作模式下替换溢出图标(和主页作为向上图标),但我希望避免这样做,只使用内置样式。
    • @Spencer 我只知道这个“解决方法”。如果我确实找到了一些优雅的东西,我会更新我的答案(并发表评论)。但是,我在发布答案之前进行了研究。系统似乎不支持您要查找的内容(还没有?)。
    • 你在这里给出的答案基本上是我在这里提出问题之前所做的(我还必须修改“home as up”图标。我希望我能让系统完成所有这些以及添加 RTL 支持等等,但我还没有弄清楚怎么做。谢谢你的想法。
    【解决方案3】:

    我只是想补充一下 Vikram 的答案,专门为动作模式下的溢出按钮和其他默认控件着色。

    在动作模式下定义这些按钮颜色的“colorControlNormal”项必须放在动作栏主题中,而不是动作模式样式中。只有这样,它才会用于操作模式中的溢出、完成、关闭和返回按钮。

    【讨论】:

      【解决方案4】:

      尝试使用

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
      </style>
      

      改为。

      【讨论】:

        猜你喜欢
        • 2021-10-16
        • 1970-01-01
        • 2022-11-24
        • 2020-04-25
        • 1970-01-01
        • 2020-08-17
        • 1970-01-01
        • 2021-01-23
        • 1970-01-01
        相关资源
        最近更新 更多