首页 > 解决方案 > 覆盖主题中的属性

问题描述

我在为我的应用程序设计主题时遇到问题。我使用 Material 主题如下

<style name="Theme.KhoaLuanTotNghiep" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

我想自定义主题,使操作栏的颜色为白色,标题栏文本为黑色。我已经跟踪了更改操作栏颜色的继承,但我无法更改标题文本的颜色。这就是我所做的:

扩展操作栏并更改其属性

<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>

要使用的文本外观:

<style name="TextAppearance.AppCompat.Widget.ActionBar.Title.Black">
    <item name="android:textColor">@color/black</item>
</style>

标签: androidandroid-actionbarstylesthemes

解决方案


在您的样式中创建以下内容,仅更改主要文本颜色。我使用purple_200(以浅紫色为例)。

<style name="Theme.MyApp.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" >

    <item name="android:textColorPrimary">@color/purple_200</item>
</style>

然后在您的 AppToolbar 中添加主题。这将更新您的应用栏中的 textColor

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.MyApp.AppBarOverlay">

您可以看到标题现在是“浅紫色”而不是默认的“白色”

在此处输入图像描述


推荐阅读