According to the Android API Guide Styles and Themes, a style can inherit from another in two different ways:
It can have a parent
attribute:
The
parent
attribute in the<style>
element lets you specify a style from which your style should inherit attributes. You can use this to inherit attributes from an existing style and define only the attributes that you want to change or add.
It can have a dotted name, as long as the style being inherited from is one "you've defined yourself":
If you want to inherit from styles that you've defined yourself, you don't have to use the
parent
. Instead, you can use dot notation by prefixing the name of the style you want to inherit to the name of your new style, separated by a period.
What happens if a <style>
has both a parent and a dotted name? For example, if I have:
<style name="Foo.Bar.Baz" parent="Pen.Pinapple.Apple.Pen">
Does Foo.Bar.Baz
inherit from both Foo.Bar
and Pen.Pinapple.Apple.Pen
? If an attribute is set in both Foo.Bar
and Pen.Pinapple.Apple.Pen
, which value will it get in Foo.Bar.Baz
? What about other cases, like an attribute being set in Pen.Pineapple
but also being set in the parent
of Foo.Bar
? Exactly what are the cascading rules, and where are they documented?
If you specify both, the parent
attribute completely overrules the "dotted parent" (i.e., anything defined in the dotted parent is ignored).
In this example, MyStyle2.SubStyle
will inherit the text color attribute from MyStyle1
and completely ignore the text size attribute from MyStyle2
:
<style name="MyStyle1">
<item name="android:textColor">#00f</item>
</style>
<style name="MyStyle2">
<item name="android:textSize">18sp</item>
</style>
<style name="MyStyle2.SubStyle" parent="MyStyle1">
<item name="android:textStyle">bold</item>
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With