总结下,免得每次忘
1)style类似于css,用来定义View等UI元素的属性,比如layout_width等,可以在layout的xml中用@style/name来引用
2)style中如果要加入自定义属性,可以在attr中定义,一个attr包括name和format属性,可以在style中用@attr/name来引用
也可以在attr中定义属性集合,用declare-styleable标签定义,这个属性集合在view的构造函数中用到
public View(Context context, AttributeSet attrs, int defStyle);
View就可以在构造函数中知道要去defStyle中的哪些属性出来
属性集的引用不同于单个属性,使用R.styleable.name来引用
3)theme一般是针对activity的,全局生效,一般在theme中指定style,而这个指定,一般由一个属性来完成,属性在attr中定义
比如attr中定义autoCompletTextViewStyle属性,然后在theme中指定
<item name="autoCompleteTextViewStyle">@android:style/Widget.AutoCompleteTextView</item>
这样就可以在View的构造函数中,利用R.attr.autoCompleteTextViewStyle取得Theme中指定的style
在 public View(Context context, AttributeSet attrs, int defStyle);
中,defStyle既可以是某一个style:R.style.name,也可以是R.attr.name,对于后者,需要在theme中指定属性值。
本文介绍了Android中如何使用styles和themes来自定义UI组件的外观。详细解释了style用于定义UI元素属性的方法,并展示了如何通过attr定义自定义属性及其集合。此外还说明了theme的作用范围及如何在theme中指定style。
691

被折叠的 条评论
为什么被折叠?



