xml文件中的theme和style属性的区别与联系
注意:这里讲的theme和style是xml中某个标签(如application标签)中的属性,而不是标签,后面要讲的style标签其实是属性值(即style属性的值)。总之,前者是属性后者(即标签)是属性值。
theme属性和style属性
<application android:theme="@style/CustomTheme">
<TextView android:style="@style/myStyle" />
style标签
<style/>
区别
作用范围不同
对于Android开发者来说Theme主要是针对窗体及Activity级别的,通常改变的是窗体的样式;而Style主要是针对窗体及Activity中具体成员元素级别的,通常改变的是控件的样式。简单粗暴理解就是Theme作用于所有(若为Theme设置的style里面为background这一属性赋值,则对所有View的background都起作用,ViewGroup和它的子成员的背景会重叠)支持属性的子成员,Style作用于指定的成员。赋值方式不同
theme的赋值方式:
系统为theme这一属性提供了多个属性值(都含有Theme),比如Theme.Light 。
使用系统自带主题赋值的方式:@android:style/
使用自定义主题赋值的方式:@style/
<!-- 使用系统预制style -->
<activity android:theme="@android:style/Theme.Dialog">
<!-- 使用系统预制但局部修改的style -->
<activity android:theme="@style/CustomTheme">
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
style属性的赋值方式:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:style="@style/myStyle" />
联系
实际上,theme实质上是style,因为theme和style属性的值都通过style 标签定义得到的。
参考:Android开发之Theme、Style探索及源码浅析
style、declare-styleable、attr这三个标签的区别与联系
注意:这三个标签分别对应R类中的R.style、R.styleable、R.attr这三个内部类。
区别
- attr
attr标签用于定义属性。attr不依赖于declare-styleable,declare-styleable只是为了方便attr的使用。
我们自己定义属性完全可以不放到declare-styleable里面,比如直接在resources文件中定义一些属性:
<attr name="custom_attr1" format="string" />
<attr name="custom_attr2" format="string" />
- declare-styleable
declare-styleable标签用于把相关的属性组织起来,有一个分组的概念。
作用:
1、属性的使用范围更加明确(比如declare-styleable标签定义的TextViewStyle,开发者通过字面意思可知其中的属性用于更改TextView样式的,因此当我们想要知道Theme具体有哪些属性可以有效使用时,可以查阅API的R.styleable进行选择,也可查SDK中android自带的attrs.xml中的declare-styleable标签)。
2、便于在创建View时获取xml或主题等中设置的属性值
<declare-styleable name="custom_attrs">
<attr name="custom_attr1" format="string" />
<attr name="custom_attr2" format="string" />
</declare-styleable>
创建View时获取属性值的方式:
TypedArray typedArray = context.obtainStyledAttributes(set,R.styleable.custom_attrs);
若是定义的属性不放到declare-styleable标签中,则在创建View时获取属性值的方式:
int[] v = {R.attr.custom_attr1, R.attr.custom_attr2};
TypedArray typedArray = context.obtainStyledAttributes(set,v);
- style
为一组attr属性赋值。