style
在布局中,每一个组件都会设置相应的属性。有些时候不同的组件会具有相同的属性和属性值,将这些相同的属性和属性值单独抽取出来组成的一组属性集合便是样式。在各个组件中可通过style属性引用已经定义好的样式(style)。样式通常放在res/values/styles.xml中,使用<style>元素进行定义。
具体使用,略。
style只作用于当前的View,ViewGroup的style不会作用于子View。
theme
主题(theme)本身也是一种样式,通常也放置在styles.xml文件中,同样使用<style>元素定义。它与style的主要区别有以下两点:
1,样式作用于单个的view,是view的一组属性的集合;而theme作用于整个activity(为某个activity单独配置theme)或者application(为整个应用配置theme)。
2,主题定义的格式通常时窗口的外面,如有无标题,是否全屏等。
theme的使用主要有两种方式:在activity或者application中,都可以通过setTheme()为该activity动态地指定主题,或者在清单文件中通过<application>或<activity>元素的theme进行设置。
常用属性
android:windowBackground 整个窗口的背景(除状态栏以外的所有区域)
android:windowFrame 该界面的前景——全覆盖该界面的所显示的内容。
android:windowNoTitle 无标题栏
android:windowFullscreen 全屏
比较
theme也是一个style,只不过使用的位置不同。theme中可以配置一些只作用于View的属性。
为Activity或Application配置theme时,该style会作用于所有的View,也就是相当于对所有的View配置了style。如:
<style name="Test">
<item name="android:textColor">@android:color/holo_red_dark</item>
<item name="android:textSize">20sp</item>
<item name="android:windowNoTitle">true</item>
</style>
当将该style作用于单个的TextView时,则会自动忽略android:windowNoTitle属性,只会根据使用textColor与textSize属性。
当作用于某个Activity或者Application时,所有的TextView都会使用textColor与textView。如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/app_name" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/app_name" />
</LinearLayout>
其效果为: