style 的繼承
- Parent
<style name="Parent"/>
- 明確的 child, 所謂的明確就是我們通過定義指出 style 繼承關係。
<style name="Child" parent="Parent"/>
- 隱含的 child
<style name="Parent.Child"/>
無論是隱式還是顯式定義繼承關係,其效果是相同的,並無不同。
我們想象一下如果在一個標籤中同時使用顯式和隱式來定義標籤繼承不同父 style 會是怎麼樣呢?
<style name="Implicit"/>
<style name="Explicit"/>
<style name="Implicit.Child" parent="Explicit"/>
選擇題
- 繼承兩者
- 繼承 Implicit
- 繼承 Explicit
正確答案是 繼承 Explicit
當隱式和顯式相遇,顯式會勝出,不過我們在開發中應該避免這樣混用隱式和顯式的情況。
當我們看到下面代碼,我們會滿心歡喜以爲 style 父級爲 Widget.Button
<Button
style="@style/Widget.Button.Calculator"
/>
不過當我們看到 style 中的定義就發現我們猜想是錯誤的。
<style
name="Widget.Button.Calculator"
parent="Widget.HahaFooledYou.button"
/>
這樣需要我們定義 style 的 name 時候也需要花一些心思,避免誤導。
那麼現在有關定義 style 名稱我給大家一點建議
<style name="BorderlessButton"
parent="Widget.AppCompact.Button.Borderless"/>
在定義具有顯式繼承的 style 的名稱是要避免使用點來分隔,用駝峯吧。
舉兩個反面的例子
<style name="Widget.Button"
parent="Widget.AppCompat.Button"/>
<style name="Widget.Button.Borderless"
parent="Widget.AppCompat.Button.Borderless"/>