【问题标题】:Android light/dark theme action bar textAndroid 浅色/深色主题操作栏文字
【发布时间】:2020-01-15 15:29:00
【问题描述】:

我在 Playground Android 应用中实现了一个深色主题,并且正在努力将操作栏文本颜色设置为白色。

以下是我的风格和颜色。操作栏的背景遵循colorPrimary,很棒。但是,两种颜色(浅色和深色)都是非常深的颜色,并且希望操作栏文本颜色始终为白色。由于我现在使用 DayNight.NoActionBar 作为父级,所以黑色表示亮,白色表示暗。我有几个不同的操作栏实例,所以我宁愿不必更改每个单独的实例,而只需在样式中定义它。我该怎么做?

styles.xml

<style name="DarkThemeApp" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorError">@color/colorError</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>

夜晚\colors.xml

<resources>
    <color name="colorPrimary">#3d85c6</color>
    <color name="colorPrimaryDark">#002e72</color>
    <color name="colorAccent">#e66f00</color>
    <color name="colorYellow">#FFE800</color>
    <color name="colorError">#E53935</color>
</resources>

值\colors.xml

<resources>
    <color name="colorPrimary">#00348e</color>
    <color name="colorPrimaryDark">#002e72</color>
    <color name="colorAccent">#e66f00</color>
    <color name="colorYellow">#FFE800</color>
    <color name="colorError">#E53935</color>
</resources>

【问题讨论】:

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


    【解决方案1】:

    使用材质主题,您有不同的选项来自定义 Toolbar 中使用的文本颜色

    • 使用 android:theme 仅覆盖 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>
    
    • 在您的Toolbar 中设置样式
    <com.google.android.material.appbar.MaterialToolbar
        style="@style/MyToolbar"
        .../>
    

    然后使用materialThemeOverlay覆盖默认值(需要材质组件库版本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>
    

    【讨论】:

    • 为我工作。我可以添加 colorOnPrimary 应该进入 colors.xml 或 theme.xml 以获得更多系统范围的功能,因为它不是仅与工具栏相关的属性。
    【解决方案2】:

    首先,将这三个添加到您的主要样式中(您可以随意命名样式):

    <item name="toolbarStyle">@style/ToolbarStyle</item>
    <item name="actionOverflowButtonStyle">@style/ToolbarStyle.Overflow</item>
    <item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>
    

    然后定义那些样式:

    <style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
        <!-- Makes the toolbar text whatever color you want for both light/dark-->
        <item name="titleTextColor">@color/actionBarText</item>
        <item name="android:background">?attr/colorPrimary</item>
    </style>
    
    <style name="ToolbarStyle.Overflow" parent="Widget.AppCompat.ActionButton.Overflow">
        <!-- For toolbar menu -->
        <item name="android:tint">@color/actionBarText</item>
    </style>
    
    <style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
        <!-- For the hamburger menu, back button, etc -->
        <item name="tint">@color/actionBarText</item>
    </style>
    

    【讨论】:

      【解决方案3】:

      在你的styles.xml中使用这个代码

      <style name="DarkThemeApp" parent="Theme.AppCompat.DayNight.DarkActionBar">
              <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
          </style>
      
          <style name="MyActionBarStyle" parent="Theme.AppCompat.DayNight.DarkActionBar">
              <item name="android:titleTextStyle">@style/MyActionBarTitleTextStyle</item>
          </style>
      
          <style name="MyActionBarTitleTextStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
              <item name="android:textColor">@color/white</item>
          </style>
      

      【讨论】:

        【解决方案4】:

        我认为这个问题的答案可能会因您在应用中使用的其他组件而异。在我的情况下,以下是一个可行的解决方案:

        styles.xml中定义这三种样式

          <style name="ToolbarStyle" parent="@style/Widget.MaterialComponents.Toolbar">
            <item name="titleTextColor">@android:color/white</item>
            <item name="android:background">@color/primary</item>
          </style>
        
          <style name="ToolbarStyle.Overflow" parent="@style/Widget.AppCompat.ActionButton.Overflow">
            <item name="android:tint">@android:color/white</item>
          </style>
        
          <style name="ToolbarStyle.DrawerIcon" parent="Widget.AppCompat.DrawerArrowToggle">
            <item name="color">@android:color/white</item>
          </style>
        

        然后将它们设置在您的主题中:

        <style name="DarkThemeApp" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
          <!-- color for stuff in action bar -->
          <item name="toolbarStyle">@style/ToolbarStyle</item>
          <item name="actionOverflowButtonStyle">@style/ToolbarStyle.Overflow</item>
          <item name="drawerArrowStyle">@style/ToolbarStyle.DrawerIcon</item>
        
          <!-- rest of your attributes go here ... -->
        </style>
        

        【讨论】:

          【解决方案5】:

          在新版本的 Android Studio 中,我们有两个主题颜色文件。要更改深色主题,您应该使用此文件:

          Res > values > themes > themes.xml (night)
          

          对于日版:

          Res > values > themes > themes.xml
          

          【讨论】:

          • 我不认为这是新的,自从引入暗模式以来,您必须使用单独的文件。
          猜你喜欢
          • 2022-08-12
          • 1970-01-01
          • 2021-12-21
          • 2020-12-21
          • 2017-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多