【背景】
想要搞懂,android中style中定义时,对于parent的写法到底是如何写的。
【折腾过程】
1.搜:
android style parent
参考:
android开发style详解 – yimigao@126的日志 – 网易博客
android style(样式)和theme(主题)设置 – 独自等待 – 博客频道 – CSDN.NET
2.之前错误写法:
写成了:
xxx
的形式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < style name = "Variable" > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > < style name = "Variable.Label" parent = "Variable" > < item name = "android:textColor" >@color/VariableLabelColor</ item > </ style > < style name = "Variable.Value" parent = "Variable" > </ style > < style name = "Variable.Value.NonEditable" parent = "Variable.Value" > < item name = "android:background" >@color/NonEditableBackgoundColor</ item > </ style > |
应该去加上@style
变成:
@style/xxx
3.也不能写成@style/xxx_yyy的形式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < style name = "Variable" > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > < style name = "Variable.Label" parent = "@style/Variable" > < item name = "android:textColor" >@color/VariableLabelColor</ item > </ style > < style name = "Variable.Value" parent = "@style/Variable" > </ style > < style name = "Variable.Value.NonEditable" parent = "@style/Variable_Value" > < item name = "android:background" >@color/NonEditableBackgoundColor</ item > </ style > |
否则会出错:
error: Error retrieving parent for item: No resource found that matches the given name ‘@style/Variable_Value’. |
应该写成:
@style/xxx.yyy
的形式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < style name = "Variable" > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > < style name = "Variable.Label" parent = "@style/Variable" > < item name = "android:textColor" >@color/VariableLabelColor</ item > </ style > < style name = "Variable.Value" parent = "@style/Variable" > </ style > < style name = "Variable.Value.NonEditable" parent = "@style/Variable.Value" > < item name = "android:background" >@color/NonEditableBackgoundColor</ item > </ style > |
【总结】
至此,才算基本明白:
的确是:
先定义一个style:
1 2 3 4 | < style name = "Variable" > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > |
然后后续在别的子的style中用parent引用,写法是:
@style/xxx
其中xxx是原先的style的名字。
1 2 3 4 5 6 7 8 9 10 | < style name = "Variable.Label" parent = "@style/Variable" > < item name = "android:textColor" >@color/VariableLabelColor</ item > </ style > < style name = "Variable.Value" parent = "@style/Variable" > </ style > < style name = "Variable.Value.NonEditable" parent = "@style/Variable.Value" > < item name = "android:background" >@color/NonEditableBackgoundColor</ item > </ style > |
即可。