Override an attribute in a theme

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-29 10:25:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!