Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 樣式與主題:像 CSS 一樣管理介面外觀

編輯:進階開發

如果你寫過網頁設計、熟悉 CSS,那麼 Android 的樣式(Style)機制你會馬上上手——兩者的精神幾乎一致。Android 的每個介面元件都可以透過屬性調整外觀:高度、內距、字型顏色、字級、背景顏色等等。

最直接的做法是把屬性寫在布局檔裡:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:capitalize="characters" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello_world" /> </LinearLayout>

問題顯而易見:同一組外觀設定散落在各個元件上,畫面一多、設計一改,維護成本會迅速失控。正確做法是把樣式抽到獨立檔案集中管理。

定義樣式

樣式定義在 res/values/ 目錄下的 XML 檔中,根節點必須是 <resources>,檔名可自訂但副檔名必須是 .xml。一個檔案裡可以放多組樣式,每組用 <style> 標籤搭配唯一的 name 識別,而具體的屬性設定則放在 <item> 標籤裡:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomFontStyle"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:capitalize">characters</item> <item name="android:typeface">monospace</item> <item name="android:textSize">12pt</item> <item name="android:textColor">#00FF00</item> </style> </resources>

<item> 的值可以是關鍵字串、十六進位顏色、另一個資源類型的參照,或其他依屬性而定的值。

使用樣式

定義好之後,在布局檔中以 style 屬性引用即可,元件標籤瞬間清爽許多:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text_id" style="@style/CustomFontStyle" android:text="@string/hello_world" /> </LinearLayout>

樣式繼承

Android 支援類似 CSS 的樣式繼承:沿用既有樣式的屬性,只覆寫你想改的部分。寫法是在名稱中以「.」串接,以下建立一個沿用 CustomFontStyle、但字級更大的樣式:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomFontStyle.LargeFont"> <item name="android:textSize">20ps</item> </style> </resources>

布局檔中即可用 @style/CustomFontStyle.LargeFont 引用它。這條鏈可以一直延伸下去,例如再從 LargeFont 衍生出紅字版本:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomFontStyle.LargeFont.Red"> <item name="android:textColor">#FF0000</item> </style> </resources>

要注意的是,這種「名稱串接」的繼承只能用在你自己定義的樣式之間,無法用來繼承 Android 內建樣式。要引用內建樣式(例如 TextAppearance),必須改用 parent 屬性:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomFontStyle" parent="@android:style/TextAppearance"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:capitalize">characters</item> <item name="android:typeface">monospace</item> <item name="android:textSize">12pt</item> <item name="android:textColor">#00FF00</item> </style> </resources>

Android 主題

理解樣式之後,主題(Theme)就好懂了。差別只在作用範圍:樣式套用在單一 View 上,主題則是套用在整個 Activity 或整個應用程式上的統一樣式。

也就是說,當一個樣式被當成主題套用時,該 Activity 或應用程式內所有支援的元件都會一併套用這些屬性。以前面的 CustomFontStyle 為例,把它設為主題後,該 Activity 裡所有文字都會變成綠色等寬字型。

要讓應用程式的所有 Activity 都套用某個主題,編輯 AndroidManifest.xml 的 <application> 標籤:

 <application android:theme="@style/CustomFontStyle">

若只想讓某一個 Activity 有專屬主題,就把 android:theme 加在該 <activity> 標籤上:

 <activity android:theme="@style/CustomFontStyle">

Android 也預先定義了一批現成主題,可以直接使用,也可以用 parent 繼承後再調整:

 <style name="CustomTheme" parent="android:Theme.Light"> ... </style>

預設樣式與主題

Android 平台本身就附帶大量樣式與主題,可以在應用程式中直接取用,全部清單可在 R.style 類別中找到。使用時把樣式名稱中的底線換成點號即可,例如要套用 Theme_NoTitleBar,就寫成 "@android:style/Theme.NoTitleBar"。

把樣式與主題用起來,介面的視覺調整就能從「逐個元件改屬性」升級為「改一處、全 App 生效」。這不只是省事,更是讓設計稿與程式碼能保持一致、日後換風格只改一個檔案的關鍵。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved