【问题标题】:How to change toolbar text color from MaterialComponents.DayNight theme?如何从 MaterialComponents.DayNight 主题更改工具栏文本颜色?
【发布时间】:2019-02-10 04:48:37
【问题描述】:

我在我的应用程序中使用 MaterialComponents.DayNight 主题。在白天模式下,工具栏文本颜色为黑色。但是当我切换到夜间模式时,工具栏的文本颜色仍然是黑色,所以它不再可见了。

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

我想在夜间模式下将工具栏文本颜色更改为白色。我该怎么做?

【问题讨论】:

  • 您找到解决方案了吗?
  • @androiddeveloper 我已经在下面发布了答案
  • 好的,谢谢。在这里,从我这里获得 +1 :)

标签: android android-toolbar android-styles material-components material-components-android


【解决方案1】:

只需在您的布局中使用(它也适用于androidx.appcompat.widget.Toolbar)样式:

<com.google.android.material.appbar.MaterialToolbar
    style="@style/Widget.MaterialComponents.Toolbar.Primary"

然后在values-night/colors.xml中定义colorOnPrimary

那么有很多选择。
您可以通过以下方式全局自定义应用主题中工具栏的样式:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  <item name="toolbarStyle">@style/MyToolbar</item>
</style>

与:

  <style name="MyToolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
    <item name="titleTextColor">@color/.....</item>
  </style>

并在values/colors.xmlvalues-night/colors.xml 中定义颜色。

或者只是在工具栏中应用一种样式

<com.google.android.material.appbar.MaterialToolbar
    style="@style/MyToolbar"

或者干脆用以下代码覆盖主题:

<com.google.android.material.appbar.MaterialToolbar
   android:theme="@style/MyThemeOverlay_Toolbar"

与:

  <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="colorOnPrimary">@color/...</item>
  </style>

【讨论】:

    【解决方案2】:

    将此条目添加到您的主题中:

    <item name="android:itemTextAppearance">@style/PopupMenuTextAppearance</item>
    

    之后,将样式相应地添加到styles.xml

    <style name="PopupMenuTextAppearance" parent="TextAppearance.AppCompat.Menu">
        <item name="android:textColor">?attr/colorOnBackground</item>
    </style>
    

    ?attr/colorOnBackgroundMaterial Components library 中可用。如果你不使用它,你应该可以使用?android:attr/textColorPrimary

    【讨论】:

      【解决方案3】:

      将您的父主题设置为parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge"

      通过使用它,您将在 MaterialComponents 的整个 DayNight 主题之上保留带有 DarkActionBar 属性的 ActionBar 的原始主题。

      【讨论】:

        【解决方案4】:

        我认为你应该在 noActionbar 上设置样式并设计新的工具栏并自定义它

        【讨论】:

          【解决方案5】:

          我在styles.xml文件中使用了这两行代码:

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

          它将工具栏文本和工具栏 X 图标的颜色更改为白色。

          整个代码如下所示:

          <resources>
              <!-- Base application theme. -->
              <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
                  <!-- Customize your theme here. -->
                  <item name="colorPrimary">#00695c</item>
                  <item name="colorPrimaryVariant">#439889</item>
                  <item name="colorOnPrimary">#ffffff</item>
                  <item name="colorSecondary">#007769</item>
                  <item name="colorSecondaryVariant">#48a697</item>
                  <item name="colorOnSecondary">#ffffff</item>
                  <item name="colorControlNormal">@android:color/white</item>
                  <item name="android:textColorPrimary">@android:color/white</item>
              </style>
          </resources>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-04-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多