【问题标题】:Android Theme.AppCompat.Light with Dark Toolbar (for light text)带有深色工具栏的 Android Theme.AppCompat.Light(用于浅色文本)
【发布时间】:2021-04-14 22:01:18
【问题描述】:

我有一个使用 Theme.AppCompat.Light 的应用程序,我需要使用它,因此对话框具有浅色主题。但这会使我工具栏中的文本变黑,而我宁愿它是白色的。我已经尝试将工具栏的特定app:theme 设置为AppCompat.Dark.Actionbar,但没有运气......我搜索了一段时间,找不到特定的问题和答案。

这是我的默认 AppTheme

<style name="AppTheme.Parent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="colorAccent">@color/pink</item>

    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

这是我的 toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

【问题讨论】:

  • 您是否尝试在工具栏上设置style 属性?
  • @Gaëtan 刚刚尝试过,同样的问题。

标签: android android-appcompat


【解决方案1】:

要更改Toolbar 中标题的颜色,您只需将属性android:textColorPrimary 添加到您的工具栏样式

有关工具栏的更多属性,请参阅以下示例。

示例

<style name="MyToolbarStyle" parent="ThemeOverlay.AppCompat.ActionBar">
    <!-- Used to for the title of the Toolbar -->
    <item name="android:textColorPrimary">#fff</item>
    <!-- Used to for the title of the Toolbar when parent is Theme.AppCompat.Light -->
    <item name="android:textColorPrimaryInverse">#fff</item>
    <!-- Used to color the text of the action menu icons -->
    <item name="android:textColorSecondary">#fff</item>
    <!-- Used to color the overflow menu icon -->
    <item name="actionMenuTextColor">#fff</item>
    <!-- Color of the Toolbar -->
    <item name="android:background">#455a64</item>
</style>

结果

【讨论】:

  • 不幸的是,这对我没有任何作用:s。出于好奇,您的最低 SDK 版本是多少?
  • 14 但这一点都不重要。您在app:theme 中正确设置了样式吗?
  • 我又试了一次,它成功了......我认为你的代码的问题是 parent="ThemeOverlay.AppCompat.ActionBar"。我使用了“parent="Theme.AppCompat.Light" 并得到了结果。将你的标记为正确,因为它本质上具有正确的代码(将 textColorPrimary 设置为专用的工具栏样式)。谢谢!
  • 很高兴听到。实际上,父级的东西是可选的 afaik。
  • 有趣...不确定它是什么/当时是什么。看来,新的工具栏代码仍然很不稳定。
【解决方案2】:

您可以自己自定义工具栏,无需设置默认文本,使用您自己的文本视图:

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary"
   android:minHeight="@dimen/abc_action_bar_default_height_material">

   <!-- Below will add your text in the center of toolbar -->
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Your Text"
        android:textColor=#ff0000
        android:textStyle="bold"/>

</android.support.v7.widget.Toolbar>

【讨论】:

  • 无需在Toolbar 中添加额外的TextView 即可更改标题的颜色。 android:textColorPrimary 已经这样做了 - 也请参阅我的回答。
  • 对于 android.support.v7.widget.Toolbar 没有名为 android:textColorPrimary 的选项。 (至少对于 API 16)
  • @elf_zwölf 不是针对工具栏本身,而是针对Style,您可以将其应用于Toolbar
【解决方案3】:

虽然自定义样式可能会起作用,但 AppCompat 库已经提供了一种简单的方法来做到这一点。如果您的 Toolbar 的背景颜色较暗/没有提供足够的对比度,请使用 `ThemeOverlay.AppCompat.Dark 作为其父级:

&lt;style name="MyTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark" /&gt;

然后相应地设置您的“工具栏”的样式:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/some_dark_color"
    android:theme="@style/MyTheme.PopupOverlay" />

Toolbars 使用颜色较浅的背景应使用 ThemeOverlay.AppCompat.Light 作为其父级。

【讨论】:

    【解决方案4】:

    2021年试试这个作品,activity android:theme="@style/AppTheme.NoActionBar" 中的第一组主题并在xml中设置主题

     <style name="AppTheme.NoActionBar">
        <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 2022-08-12
      • 2023-03-08
      相关资源
      最近更新 更多