当样式同时具有父属性和带点的名称时,继承行为如何?
How does inheritance behave when a style has both a parent attribute and a dotted name?
根据 Android API 指南 Styles and Themes,一种样式可以通过两种不同的方式从另一种继承:
它可以有一个parent
属性:
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.
它可以有一个点名,只要被继承的样式是一个 "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.
如果 <style>
既有父名称又有点分名称会怎样?例如,如果我有:
<style name="Foo.Bar.Baz" parent="Pen.Pinapple.Apple.Pen">
Foo.Bar.Baz
是否同时继承了 Foo.Bar
和 Pen.Pinapple.Apple.Pen
?如果在 Foo.Bar
和 Pen.Pinapple.Apple.Pen
中都设置了一个属性,那么它将在 Foo.Bar.Baz
中得到哪个值?其他情况呢,比如在 Pen.Pineapple
中设置但也在 Foo.Bar
的 parent
中设置的属性?究竟什么是级联规则,它们记录在哪里?
如果同时指定两者,parent
属性将完全否决 "dotted parent"(即忽略在虚线父项中定义的任何内容)。
在此示例中,MyStyle2.SubStyle
将继承 MyStyle1
的文本颜色属性并完全忽略 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>
根据 Android API 指南 Styles and Themes,一种样式可以通过两种不同的方式从另一种继承:
它可以有一个
parent
属性: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.它可以有一个点名,只要被继承的样式是一个 "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.
如果 <style>
既有父名称又有点分名称会怎样?例如,如果我有:
<style name="Foo.Bar.Baz" parent="Pen.Pinapple.Apple.Pen">
Foo.Bar.Baz
是否同时继承了 Foo.Bar
和 Pen.Pinapple.Apple.Pen
?如果在 Foo.Bar
和 Pen.Pinapple.Apple.Pen
中都设置了一个属性,那么它将在 Foo.Bar.Baz
中得到哪个值?其他情况呢,比如在 Pen.Pineapple
中设置但也在 Foo.Bar
的 parent
中设置的属性?究竟什么是级联规则,它们记录在哪里?
如果同时指定两者,parent
属性将完全否决 "dotted parent"(即忽略在虚线父项中定义的任何内容)。
在此示例中,MyStyle2.SubStyle
将继承 MyStyle1
的文本颜色属性并完全忽略 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>