【问题标题】:Style selector?样式选择器?
【发布时间】:2013-07-24 18:02:50
【问题描述】:

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

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

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

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

编辑

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

【问题讨论】:

  • “活跃风格”是什么意思?
  • @simon 我更新了我的问题

标签: android android-layout styles


【解决方案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"
[...] />

【讨论】:

  • 谢谢,但 attr 解决方案不起作用,我得到以下信息:Error inflating class
猜你喜欢
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 2011-09-11
  • 2017-02-27
相关资源
最近更新 更多