且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置基于主题颜色

更新时间:2023-01-27 11:43:34

我同样的问题,并努力接近做类似的事情是什么,你想出但幸运的是我发现了以下的解决方案,我还指出here.

I struggled with the same question and was close to do something similar to what you came up with yet luckily i found the following solution which I also stated here.

首先,您在attr.xml定义自定义颜色域

First you define the custom color fields in attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="titleColor" format="reference|color" />
<attr name="introColor" format="reference|color" />

</resources>

下面定义你的主题

Next you define your themes

<style name="AppTheme.MyLight" parent="android:Theme">
   <item name="titleColor">#FFFFFF</item>
   <item name="introColor">#FFFFAA</item>
</style>


<style name="AppTheme.MyDark" parent="android:Theme">
   <item name="titleColor">#000000</item>
   <item name="introColor">#004444</item>
</style>

终于在布局

<TextView
    android:id="@+id/quoteTitle"
    android:textColor="?titleColor"
    ...
</TextView>

<TextView
    android:id="@+id/quoteIntro"
    android:textColor="?introColor"
    ...
</TextView>

似乎有关于使用属性Android官方文档中没有解释。我发现***的资源就是这里