微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

覆盖主题中的属性

如何解决覆盖主题中的属性

我在为我的应用设置主题样式时遇到问题。我使用 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>

解决方法

在您的样式中创建以下内容,仅更改主要文本颜色。我用的紫色_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">

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

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。