gpt4 book ai didi

android - 样式选择器?

转载 作者:行者123 更新时间:2023-11-29 01:51:20 25 4
gpt4 key购买 nike

我有两个基于全息的 ABS 样式,紫色和石灰,用户可以从设置中设置主题。

在我的布局中,我有一个带有自定义 textAppearance 的 TextView,我想根据 Activity 样式更改该 textAppearance。

(如果激活了紫色主题,则文本必须为白色,如果激活了石灰主题,则文本必须为石灰)

有没有办法从 XML 中做到这一点?

编辑

如果标题有误导性,我很抱歉。

最佳答案

(if the purple theme is activated the text must be white and if the lime theme is activated the text must be lime)

试试看:

<style name="PurpleTheme" parent="...">
<!-- define the style for the text appearance here. Following is an example -->
<item name="android:textAppearance">@style/MyWhiteText</item>
</style>

<style name="LimeTheme" parent="...">
<!-- define the style for the text appearance here. Following is an example -->
<item name="android:textAppearance">@style/MyLimeText</item>
</style>

<style name="MyWhiteText" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/white</item>
</style>

<style name="MyWhiteText" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/lime</item>
</style>

或者,如果您不想将颜色设置为所有 TextView,则执行此操作:

声明一个属性:

<attr name="myTextViewColor" format="reference" />

在你的主题中像这样使用它:

<style name="PurpleTheme" parent="...">
<item name="myTextViewColor">@color/white</item>
</style>

<style name="LimeTheme" parent="...">
<item name="myTextViewColor">@color/lime</item>
</style>

现在您可以像这样设置特定 TextView 的颜色:

<TextView 
android:textColor="?attr/myTextViewColor"
[...] />

关于android - 样式选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17841526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com