[英]Android light/dark theme action bar text
我正在我的 Playground Android 應用程序中實現一個深色主題,並且正在努力使操作欄文本顏色變為白色。
下面是我的款式和顏色。 操作欄的背景跟隨着colorPrimary,很棒。 但是,兩種顏色(淺色和深色)都是非常深的顏色,並且希望操作欄文本顏色始終為白色。 由於我現在使用 DayNight.NoActionBar 作為父級,所以它是黑色的淺色和白色的深色。 我有幾個不同的操作欄實例,所以我不想改變每個單獨的實例,而只是在樣式中定義它。 我該怎么做?
樣式文件
<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>
值\\顏色.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>
使用材質主題,您可以使用不同的選項來自定義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>
首先,將這三個添加到您的主要樣式中(您可以隨意命名樣式):
<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>
在你的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>
我認為這個問題的答案可能會因您在應用程序中使用的其他組件而異。 在我的情況下,以下是一個有效的解決方案:
在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>
在新版本的 Android Studio 中,我們有兩個主題顏色文件。 要更改深色主題,您應該使用此文件:
Res > values > themes > themes.xml (night)
對於日版:
Res > values > themes > themes.xml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.