1.什么是Style及Theme?两者有何区别
(1) Theme是针对窗体级别的,改变窗体样式的,如窗口标题、边框等。不能作用于单个View组件,是对整个应用的所有Activity或单个Activity起作用。
(2) Style是针对窗体元素级别的,改变指定控件或者Layout的样式
使用Style
(1)创建Style
(1) 在res\values\下创建styles.xml文件
(2)添加<resources>节点(根节点)
(3)添加自定义的style及其属性
style的写法通常为:
<style name="MyStyle" [parent="PARENT"]>
<item name="[ATTR]">[VALUE]</item>
</style>
其中,parent属性为其父style的名字(可选),通过设置该值,可继承其它style的属性。当我们需要对现有的style的属性。当我们需要对现有的style做微小的改变
(2)为控件指定Style
<EditText android:layout_height="wrap_content"
android:text="EditText"
style="@style/Title"
android:layout_width="fill_parent"
android:id="@+id/editText1"></EditText>
Theme的使用
1.在setContentView()方法之前设置Theme:setTheme(R.style.XXX);
2.在mainfest文件中加入Theme Theme就是Style
AndroidManifest.xml中:
<application android:theme="@android:style/theme">,
<activity android:theme="@android:style/theme">,
在R.attr定义中以window开头的一些属性只对theme有效。
如果一个应用使用了theme,同时应用下的view也使用了style,那么当theme与样式style发生冲突时,style的优先级高于主题。