android 主题 - 在自定义主题中定义颜色 [英] android themes - defining colours in custom themes
问题描述
我确定有一个简单的答案,但我找不到它,所以我把它扔进了stackoverflow ...;-)
I am sure there is a simple answer to that yet I just cant find it so I throw it into stackoverflow ... ;-)
我只是举个例子.我有一个 android 应用程序,用户可以在其中选择首选项中的主题 - 深色或浅色主题.根据选择的主题,我必须在我的应用程序中调整 20 种颜色.所以我希望我可以在主题中定义颜色,然后在我的 TextViews 等中使用这种定义颜色的名称.但到目前为止我不知道如何做到这一点,也找不到任何解决方案.我真的不想为这 20 种颜色中的每一种都定义额外的明暗样式,但到目前为止,这似乎是我能找到的唯一解决方案.
I will just put it into an example. I have an android app where the user can choose the theme in the preferences - dark or light theme. Depending on the chosen theme I have to adjust 20 colors in my app. So I have the hope that I can define colours in the theme and then use the names of this so defined colours in the my TextViews etc. Yet so far I cant figure out how to do that and can't find any solution here and there. I really dont want to define an extra dark and light style for each of these 20 colours yet so far that seems the only solution I can find.
非常感谢您的任何提示
马丁:
更新:
在伪语法中,这就是我正在寻找的.可能吗?
In pseudo syntax is that is what I am looking for. Is it possible?
<style name="AppTheme.MyDark" parent="android:Theme">
-?-> titleColor = "#ffffff"
-?-> introColor = "#ffaaaa"
</style>
<style name="AppTheme.MyLight" parent="android:Theme.Light">
-?-> titleColor = "#000000"
-?-> introColor = "#004444"
</style>
<TextView
android:id="@+id/quoteTitle"
android:textColor=@titleColor
...
</TextView>
<TextView
android:id="@+id/quoteIntro"
android:textColor=@introColor
...
</TextView>
推荐答案
我找到了一个似乎有效的解决方案.首先需要在 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
这篇关于android 主题 - 在自定义主题中定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!