如何从MaterialComponents.DayNight主题更改工具栏文本颜色? [英] How to change toolbar text color from MaterialComponents.DayNight theme?
问题描述
我在我的应用程序中使用MaterialComponents.DayNight主题.在白天模式下,工具栏文本颜色为黑色.但是当我切换到夜间模式时,工具栏的文本颜色仍为黑色,因此不再可见.
I am using MaterialComponents.DayNight theme in my app. In the day mode, toolbar text color is black. But when I switch to night mode toolbar text color is remain black, so it's not visible anymore.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
我想在夜间模式下将工具栏文本颜色更改为白色.我该怎么办?
I want to change the toolbar text color into white in night mode. How can I do that?
推荐答案
仅在您的布局中使用(它也与androidx.appcompat.widget.Toolbar
一起使用)样式:
Just use in your Layout (it works also with the androidx.appcompat.widget.Toolbar
) the style:
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
然后在values-night/colors.xml
colorOnPrimary
中定义.
Then define in the values-night/colors.xml
the colorOnPrimary
.
然后有很多选择.
您可以使用以下方法在应用程序主题中全局自定义工具栏的样式:
Then there are a lot of alternatives.
You can customize globally the style of the toolbar in the app theme with:
<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.xml
和values-night/colors.xml
中定义颜色.
或者仅在工具栏中应用样式
Or just apply a style in the Toolbar
<com.google.android.material.appbar.MaterialToolbar
style="@style/MyToolbar"
或简单地使用以下主题覆盖
or simply override the theme with:
<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>
这篇关于如何从MaterialComponents.DayNight主题更改工具栏文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!