【问题标题】:How to use light Action Mode with DarkActionBar?如何在 DarkActionBar 中使用轻量动作模式?
【发布时间】:2016-02-09 00:40:32
【问题描述】:

这是我的主题。我正在使用DarkActionBar,因此标题、后退按钮和溢出图标是白色而不是黑色。

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

However, when text is selected, the ActionMode / Contextual Action Bar is white text on a 'black' background.

当我删除.DarkActionBar 时,上下文操作栏会切换为浅灰色上的黑色,这对这个应用程序来说更令人愉悦。所以如果我要走那条路,我需要找到一种不同的方法来让主操作栏使用白色前景。

如何在 ActionMode/上下文操作栏上获得浅色背景/深色前景,同时我的主 ActionBar 上仍具有浅色前景?


这是我根据丹尼尔在 cmets 中的建议所尝试的:

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="actionModeStyle">@style/MyTheme.ActionMode</item>
    <item name="actionMenuTextColor">@android:color/black</item>
</style>

<style name="MyTheme.ActionMode" parent="Widget.AppCompat.ActionMode">
    <item name="background">@android:color/white</item>
    <item name="actionMenuTextColor">@android:color/black</item>
    <item name="android:titleTextStyle">@style/MyTheme.ActionMode.TitleText</item>
    <item name="android:subtitleTextStyle">@style/MyTheme.ActionMode.TitleText</item>
</style>

<style name="MyTheme.ActionMode.TitleText">
    <item name="android:textColor">@android:color/black</item>
</style>

不幸的是,我所能改变的只是背景。所有 ActionMode 文本和图标仍然是白色的。

我现在将使用&lt;item name="background"&gt;@color/colorPrimary&lt;/item&gt;。它比黑色背景更好。

似乎还是有办法将前景更改为黑色,让我可以使用 Android 典型的浅灰色背景。

【问题讨论】:

  • 你试过设置actionModeStyle吗?看看这里接受的答案:stackoverflow.com/questions/27458421/…
  • @DanielNugent,查看我的编辑。
  • 嗯,实际上看起来应该可以。奇怪...
  • 哎呀!忘记将actionMenuTextColor 添加到我的values-v21\styles.xml 覆盖。所以,所有这些改变颜色的尝试,只有第一个是必要的&lt;item name="actionMenuTextColor"&gt;@android:color/black&lt;/item&gt;。我不需要titleTextStyle 等。看起来我必须将剪贴画图标打包到我的项目中,因为它们仍然是白底白字。似乎 Android 会有更直接的解决方案,但也许没有。
  • 不错!你应该用你的最终配置写一个答案。请注意,您可以在此处找到大多数内容的图标,包括浅色和深色选项:design.google.com/icons

标签: android themes android-actionmode


【解决方案1】:

我已经找到了答案,但它非常手动:

您可以为每个图像添加您自己的所有图形,以适合您的主题风格:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Override the ActionMode Style -->
        <item name="actionModeStyle">@style/ActionMode</item>

        <item name="actionModeCloseDrawable">@drawable/ic_action_navigation_close</item>

        <!-- Styles inherited from the Base Theme -->
        <item name="android:actionModeCutDrawable">@drawable/ic_action_content_content_cut</item>
        <item name="android:actionModeCopyDrawable">@drawable/ic_action_content_content_copy</item>
        <item name="android:actionModePasteDrawable">@drawable/ic_action_content_content_paste</item>
        <item name="android:actionModeSelectAllDrawable">@drawable/ic_action_content_select_all</item>

    </style>
    <!-- Action Mode -->
    <style name="ActionMode" parent="Widget.AppCompat.ActionMode">
        <item name="background">@android:color/white</item>
        <item name="titleTextStyle">@style/TitleTextStyle</item>

    </style>

    <!-- Title text of Contextual action bar-->
    <style name="TitleTextStyle">
        <item name="android:textColor">@android:color/darker_gray</item>
        <item name="android:textSize">18sp</item>
    </style>

</resources>

幸运的是,Roman Nurik 为我们提供了一个很好的工具来获取我们需要的所有资产:

http://romannurik.github.io/AndroidAssetStudio/icons-actionbar.html#source.space.trim=1&source.space.pad=0&name=ic_action_example&theme=light&color=33b5e5%2C60

只需使用剪贴画选项(搜索剪切、复制、过去、全选和关闭)

祝你好运,编码愉快!

【讨论】:

  • 谢谢。 android Studio 中有一个来自剪贴画工具的非常好的图像资源,所以它不会那么耗时。我只是希望 Android 能为此提供更直接的机制。
  • 也许我应该专注于更改常规操作栏的前景,这样我就可以删除.DarkActionBar 选项。这样只有两个图像可以替换(返回,溢出),不包括我的自定义图标。 (反正已经是浅色前景了)
  • 看起来 Roman Nurik 的剪贴画比 Android Studio 多。 :-)
  • @GeorgeBailey 这是一种选择,或者您可以仅使用工具栏以及该小部件样式中可用的着色选项
猜你喜欢
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
相关资源
最近更新 更多