更新时间:2023-01-26 19:09:15
我找到了一个似乎有效的解决方案.首先需要在 attr.xml 中定义自定义颜色字段
I found a solution which seems to work. First you need to 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>
接下来定义主题
<style name="AppTheme.MyDark" parent="android:Theme">
<item name="titleColor">#FFFFFF</item>
<item name="introColor">#FFFFFF</item>
</style>
<style name="AppTheme.MyLight" 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官方文档中似乎没有关于使用属性的解释.我找到的***资源是这里
There seems to be no explanation in the official android documentation about using attributes. Best resource I found is here