问题
I'm having trouble styling a theme for my app. I use Material theme as following
<style name="Theme.KhoaLuanTotNghiep" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
I want to custom the theme so that the action bar's color is white and title bar text is black. I have traced the inheritance to change the action bar's color but I cant change the color for the title text. This is what I have done:
extends the action bar and change its attributes
<style name="Widget.App.ActionBar" parent="Widget.MaterialComponents.ActionBar.Primary">
<item name="background">@color/white</item>
<item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Black</item>
</style>
Text appearance to use:
<style name="TextAppearance.AppCompat.Widget.ActionBar.Title.Black">
<item name="android:textColor">@color/black</item>
</style>
回答1:
create the following in your styles with just the primary text color changed. I use purple_200 (light purple as an example).
<style name="Theme.MyApp.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" >
<item name="android:textColorPrimary">@color/purple_200</item>
</style>
then in your AppToolbar add the theme in it. This will update textColor in your appbar
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.MyApp.AppBarOverlay">
you can see that the title is now "light purple" instead of the default "white"
来源:https://stackoverflow.com/questions/65704673/override-an-attribute-in-a-theme