且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android浅色/深色主题操作栏文本

更新时间:2023-01-27 17:35:16

使用材质主题,您可以使用不同的选项来自定义工具栏

Using a material theme you have different options to customize the text color used in the Toolbar

  • 使用 android:theme 覆盖仅用于 Toolbar
  • 的默认值
  • Use the android:theme to override default values only for your Toolbar
<com.google.android.material.appbar.MaterialToolbar
    android:theme="@style/MyThemeOverlay_Toolbar"
    ...>

并使用:

  <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <!-- color used by the toolbar title -->
    <item name="android:textColorPrimary">@color/secondaryColor</item>
    <!-- color used by navigation icon and overflow icon -->
    <item name="colorOnPrimary">@color/secondaryColor</item>
  </style>

  • 在您的工具栏
  • 中设置样式
<com.google.android.material.appbar.MaterialToolbar
    style="@style/MyToolbar"
    .../>

然后使用 materialThemeOverlay 覆盖默认值(它需要材料组件库版本1.1.0):

And then use the materialThemeOverlay to override the default values (it requires the material components library version 1.1.0):

  <!-- Toolbar -->
  <style name="MyToolbar" parent="Widget.MaterialComponents.Toolbar">
    <item name="materialThemeOverlay">@style/MyThemeOverlay_Toolbar</item>
  </style>

  <style name="MyThemeOverlay_Toolbar" parent="">
    <item name="android:textColorPrimary">@color/secondaryColor</item>
    <item name="colorOnPrimary">@color/secondaryColor</item>
  </style>