风格选择器?

时间:2013-07-24 18:02:50

标签: android android-layout styles

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

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

(如果紫色主题被激活,文字必须是白色的,如果石灰主题被激活,文字必须是石灰)

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

修改

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

1 个答案:

答案 0 :(得分:1)

  

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

试试:

<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>

或者,如果您不想将颜色设置为 all TextViews,请执行以下操作:

声明属性:

<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"
[...] />