GPinskiy
GPinskiy

Reputation: 307

Using A Custom Attribute That Is In A Style With A View

Lets say I have a custom attribute in a few styles like so:

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

Can I then reference that in a standard view like a TextView? Something like:

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

Upvotes: 0

Views: 33

Answers (1)

James McCracken
James McCracken

Reputation: 15766

You need to define a TextView style, like this:

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

Then you won't even need to set the android:background attribute on each TextView in your xml, it will apply to all TextViews that do not specify a different background.

Upvotes: 1

Related Questions