17

I have a TextView and I want to apply a Style which I use for all TextView elements plus another style which I only use within a specific Activity. Is there any possibility to do that?

Bruiser
  • 11,767
  • 5
  • 34
  • 45

4 Answers4

23

Just a little piece of information that might add to the overall value of the question - shamelessly copied from: http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:

  <style name="CodeFont.Red">
        <item name="android:textColor">#FF0000</item>
    </style>

Notice that there is no parent attribute in the tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as @style/CodeFont.Red.

You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:

<style name="CodeFont.Red.Big">
    <item name="android:textSize">30sp</item>
</style>
Bruiser
  • 11,767
  • 5
  • 34
  • 45
  • 4
    I find it interesting that, even though I have previously visited and studied/skimmed that page, I did not know this interesting and _very_ useful tidbit. Thanks @Bruiser. – DavidDraughn Jan 12 '12 at 02:36
14

A style under Android can have a parent style.

So just have MyActivityTextView define GeneralTextView as parent style, and change/add style properties.

Then you can use MyActivityTextView for some views and GeneralTextView for the others.

It's described in Defining Styles.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
4

You can extend one style with another in your style.xml:

<style name="current_weekday_white" parent="current_day_white">
    <item name="android:textColor">#FFABAB</item>
</style>
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • hey there i have tried this with text view but only the first style works, not the extended one. any clues? – JRE.exe Nov 22 '14 at 04:30
0

Inherit one style from another and copy elements from third.

<style name="Button.Style1" parent="android:style/Widget.Button">
    <item name="android:gravity">center</item>
    <item name="android:textSize">12sp</item>
    <item name="android:background">@drawable/shape_button</item>

    <!-- Here are attributes from third style -->
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textColor">#112233</item>
</style>
CoolMind
  • 26,736
  • 15
  • 188
  • 224