53

Is there any way to make a style inherit from multiple other styles, instead of just being limited to:

<style name="WidgetTextBase">
    <item name="android:typeface">serif</item>
    <item name="android:textSize">12dip</item>
    <item name="android:gravity">center</item>
</style>

<style name="BOSText" parent="WidgetTextBase">
    <item name="android:textColor">#051C43</item>
</style>

I would like BOSText to also inherit from:

<style name="WidgetTextHeader">
    <item name="android:textStyle">bold</item>
<style>
NPike
  • 13,136
  • 12
  • 63
  • 80
  • Is this maybe useful? http://stackoverflow.com/questions/4851175/how-to-apply-two-different-styles-to-one-element-in-android/4893402#4893402 – Bruiser Nov 28 '14 at 00:12

4 Answers4

43

Styles do not support multiple inheritance (at least not as of Android 3.2).

The official docs say:

If you use the dot notation to extend a style, and you also include the parent attribute, then the parent styles override any styles inheritted through the dot notation.

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
Jonas
  • 2,126
  • 21
  • 16
  • 6
    Sorry, but You are wrong. See link http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles – Roger Alien Mar 04 '12 at 02:33
  • 29
    You misunderstood the question (see my comment on your answer below). You are right that you can construct style inheritance chains like A > B > C. The question though was whether you could do (A, B) > C. I.e., a style inherits from two independent inheritance chains. – Jonas Mar 05 '12 at 20:51
  • 14
    Why is this set as the accepted answer? Styles DO support multiple inheritance. The `parent` attribute gives you ONE inheritance and you can get a second inheritance from the name of the style, for instance: `` will inherit from the style called `Text`. From the documentation: `Note: If you use the dot notation to extend a style, and you also include the parent attribute, then the parent styles override any styles inheritted through the dot notation.` – Darwind Oct 02 '18 at 11:54
  • 5
    @Darwind This is wrong. You cannot inherit from multiple styles. When you use both dot notation and the parent attribute, **all** the styles from the dot notation is not applied **at all** regardless of the presence of the styles in the parent-attribute's style. – Richard Apr 06 '20 at 12:35
  • This answer is right. Excerpt from docs: "If you use the dot notation to extend a style, and you also include the parent attribute, then the parent styles override any styles inheritted through the dot notation." – WindRider Apr 14 '20 at 18:57
  • Damn that's an unclear message. – Barry Fruitman Mar 21 '22 at 21:32
11

You can only inherit one style. However, you can also make the inherited style inherit from another style, and so on:

<style name="WidgetTextBase">
    <item name="android:typeface">serif</item>
    <item name="android:textSize">12dip</item>
    <item name="android:gravity">center</item>
</style>

<style name="WidgetTextHeader" parent="WidgetTextBase">
    <item name="android:textStyle">bold</item>
</style>

<style name="BOSText" parent="WidgetTextHeader">
    <item name="android:textColor">#051C43</item>
</style>

You can't inherit more than one style, but you can set up an inheritance chain.

Odys
  • 8,951
  • 10
  • 69
  • 111
Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68
5

For those who was looking for solution to just merge multiple different styles into one, you can use

public void applyStyle (int resId, boolean force)

https://developer.android.com/reference/android/content/res/Resources.Theme#applyStyle(int,%20boolean). And apply it that way

context.theme.applyStyle(R.style.MyAdditionalStyle, false)

Whenever you specify true as second argument, it overrides existing values in your theme, and when false it adds only non-overlapping values from R.style.MyAdditionalStyle

I haven't tested scenario with multiple styles yet, but according to docs you can achieve it. So that's how this approach can be used as an alternative to multiple inheritance.

Ivan Syabro
  • 136
  • 1
  • 6
-4

There is a parent attribute on the style tag that should let you inherit from other styles...

i.e.

<style name="CodeFont" parent="@style/WidgetTextBase">
   <item name="android:textStyle">bold</item>
</style>

http://developer.android.com/guide/topics/ui/themes.html

f0ster
  • 549
  • 3
  • 12