Android 使用视图样式中的自定义属性

Android 使用视图样式中的自定义属性,android,android-layout,Android,Android Layout,假设我有一些样式中的自定义属性,如下所示: <style name="Theme1" parent="android:Theme.Holo"> <item name="textViewBackground">#000022</item> <item name="android:background">#000000</item> </style> <style name="Theme2" paren

假设我有一些样式中的自定义属性,如下所示:

<style name="Theme1" parent="android:Theme.Holo">
    <item name="textViewBackground">#000022</item>
    <item name="android:background">#000000</item>
  </style>
<style name="Theme2" parent="android:Theme.Holo">
    <item name="textViewBackground">#AA2200</item>
    <item name="android:background">#000000</item>
  </style>

#000022
#000000
#AA2200
#000000
然后,我可以在标准视图(如TextView)中引用它吗?比如:

<TextView
    android:id="@+id/txtNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@CurrentTheme.textViewBackground"
    android:text="Number" />

您需要定义文本视图样式,如下所示:

<style name="Theme1" parent="android:Theme.Holo">
    <item name="android:textViewStyle">@style/TextViewStyle1</item>
    <item name="android:background">#000000</item>
</style>

<style name="Theme2" parent="android:Theme.Holo">
    <item name="android:textViewStyle">@style/TextViewStyle2</item>
    <item name="android:background">#000000</item>
</style>

<style name="TextViewStyle1" parent="@android:style/Widget.TextView">
    <item name="android:background">#000022</item>
</style>

<style name="TextViewStyle2" parent="@android:style/Widget.TextView">
    <item name="android:background">#AA2200</item>
</style>

@样式/文本视图样式1
#000000
@样式/文本视图样式2
#000000
#000022
#AA2200

然后,您甚至不需要在xml中的每个
TextView
上设置
android:background
属性,它将应用于所有未指定不同背景的
TextView
s。

我希望有一个主题来定义一整组全局变量,而不是为每个元素创建单独的主题。哦,好吧。谢谢