一、樣式
樣式是屬性的集合,例如定義屬性fontColor、fontSize、layout_width、layout_height等,以獨立的資源檔存放在XML檔案中,並設定樣式的名稱。
Android Style類似網頁設計中的級聯樣式CSS設計思路,可以讓設計與內容分離,並且可以方便的繼承、覆蓋、重用。
1.未使用Style
<?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內容如下:
<?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對應屬性的值。
使用樣式:
<?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
<?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"
二、通過命名規則實現
<?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活動的主題
<application android:theme="@style/wrap_content">
這樣,應用中所有Activity中的所有組件都會預設使用包裹布局。
2.設定某個指定的Activity主題
<activity android:theme="@style/wrap_content">
另外,android提供了許多內建的主題樣式。例如Theme.Dialog、Theme.Translucent等等。使用方式也很簡單
<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