一、樣式
樣式是屬性的集合,例如定義屬性fontColor、fontSize、layout_width、layout_height等,以獨立的資源文件存放在XML文件中,並設置樣式的名稱。
Android Style類似網頁設計中的級聯樣式CSS設計思路,可以讓設計與內容分離,並且可以方便的繼承、覆蓋、重用。
1.未使用Style
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:text="@string/hello" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:text="@string/hello" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>上述界面中有三個控件,2個TextView、1個Button。他們的佈局寬度layout_width與佈局高度layout_height都是wrap_content包裹內容。
下面看看,如何使用Style來改進。
2、使用Style
首先,在res/values/下創建Style XML資源文件,這裡創建的Style資源文件名命名為styles.xml,這個可以自己自定義。
styles.xml內容如下:
[html] <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>其中,style標簽中name屬性類似CSS中的class name,item標簽中的name對應屬性的名字,item標簽對中的text對應屬性的值。
使用樣式:
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
style="@style/wrap_content"
android:textColor="#00FF00"
android:text="@string/hello" />
<TextView
style="@style/wrap_content"
android:text="@string/hello" />
<Button
style="@style/wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
style="@style/wrap_content"
android:textColor="#00FF00"
android:text="@string/hello" />
<TextView
style="@style/wrap_content"
android:text="@string/hello" />
<Button
style="@style/wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
3、樣式的繼承
有兩種方式來實現繼承,一是通過style的parent屬性,二是使用類似CSS中的命名規則來實現。
一、通過parent屬性
修改styles.xml
[html] <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="inherit" parent="wrap_content">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="inherit" parent="wrap_content">
<item name="android:textColor">#00FF00</item>
</style>
</resources>新增名為inherit的樣式,並且繼承名為wrap_content樣式,也就是說inherit具有wrap_content樣式中定義的屬性參數。
引用方式:style="@style/inherit"
二、通過命名規則實現
[html] <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="wrap_content.inherit">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="wrap_content.inherit">
<item name="android:textColor">#00FF00</item>
</style>
</resources>通過“.”號實現繼承。
引用方式:style="@style/wrap_content.inherit"
二、主題
針對應用中所有Activity或者針對某個Activity設置樣式,可以通過編輯AndroidManifest.xml來完成。
1.設置應用中所有Activity活動的主題
[html] <application android:theme="@style/wrap_content">
<application android:theme="@style/wrap_content">這樣,應用中所有Activity中的所有組件都會默認使用包裹佈局。
2.設置某個指定的Activity主題
[java] <activity android:theme="@style/wrap_content">
<activity android:theme="@style/wrap_content">
另外,android提供瞭許多自帶的主題樣式。例如Theme.Dialog、Theme.Translucent等等。使用方式也很簡單
[html] <activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@android:style/Theme.Dialog">
樣式屬性參考:http://android.toolib.net/reference/android/R.attr.html
主題屬性參數:http://android.toolib.net/reference/android/R.styleable.html#Theme
樣式與主題參考:http://android.toolib.net/guide/topics/ui/themes.html
摘自 God's blog