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

Android Styles 與 Themes:樣式和主題怎麼定義、怎麼套用

編輯:Android 開發實例

Style 與 Theme 的分別

Style(樣式)是一組格式化屬性的集合,可以當成一個單位套用到版面 XML 中的單一元素。例如定義一種樣式來固定文字大小與顏色,然後套用在某個 View 上。

Theme(主題)同樣是一組屬性集合,但作用範圍是整個應用程式的所有 Activity,或指定某一個 Activity。例如定義一個 Theme 來決定 window 外框與 panel 的前景/背景顏色、選單文字大小與顏色,套上之後所有 Activity 的外觀都會跟著改變。

一句話總結:style 作用於 view,theme 作用於 application 或 activity。

定義的 XML 結構

每個 style 或 theme 都在 <style> 元素上取一個全域唯一的 name,也可選擇加上 parent 屬性來表明它繼承自哪個樣式。在 <style> 內部則放一個或多個 <item>,每個 item 用 name 指定要覆寫的屬性,內容就是該屬性的值。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 定義 style -->
    <style name="myTextStyle">
        <item name="android:textSize">20px</item>
        <item name="android:textColor">#EC9237</item>
    </style>

    <style name="myTextStyle2">
        <item name="android:textSize">14px</item>
        <item name="android:textColor">#FF7F7C</item>
    </style>

    <!-- 定義 theme -->
    <style name="myTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:textSize">14px</item>
        <item name="android:textColor">#FFFF7F7C</item>
    </style>
</resources>

從寫法可以看出,定義 style 與 theme 幾乎一模一樣,差別只在套用位置與部分專屬屬性(例如 windowNoTitle 這類只對 Activity/theme 有意義)。

在版面中套用

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        style="@style/myTextStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="hello" />

    <TextView
        style="@style/myTextStyle2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="www.google.cn"
        android:autoLink="all" />
</LinearLayout>

這份版面同時用了 style 與 theme(theme 在 Activity 或 manifest 中套用)。兩者若同時定義了同一個屬性就會衝突,實測後的結論是 style 的優先級高於 theme——因為 style 是直接綁在該 View 上的,比全域性的 theme 更接近元素本身。

順帶一提 android:autoLink:加上這個屬性後,該 View 的文字會變成藍色帶底線的可點擊連結,點擊時會呼叫系統瀏覽器開啟網址。

套用 Theme 的三種方式

1. 程式碼中動態套用,注意必須在 setContentView 之前呼叫:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.myTheme);
    setContentView(R.layout.main);
}

2. 套用到整個應用程式:在 AndroidManifest.xml 的 <application> 標籤加上 android:theme 屬性,例如 <application android:theme="@style/NewTheme">。

3. 只套用到某個 Activity:修改該 <activity> 標籤。Android 本身內建了不少現成主題可直接切換,例如讓 Activity 看起來像對話框:<activity android:theme="@android:style/Theme.Dialog">。

用 parent 繼承並微調內建主題

如果喜歡某個內建主題,只想做小幅調整,把它設為 parent 即可:

<style name="NewDialogTheme" parent="@android:style/Theme.Dialog">
    <!-- 覆寫想改的 item -->
</style>

繼承之後,你可以逐一覆寫 Theme.Dialog 中定義的 item 值,然後在 manifest 裡改用 NewDialogTheme 取代 Theme.Dialog,就能得到一個「大致相同、細節不同」的新主題。

補充:官方文件對兩者的界定

官方文件特別提醒,自訂 style 在套用時前面不加 android: 命名空間(寫成 style="@style/xxx"),因為它是應用程式自己的資源,而非平台提供的資源。

Themes can be defined in exactly the same <style> and <item> structure as styles are. To apply a theme you simply associate a style with an entire Activity, such as android:theme="@android:style/[stylename]".

實務上的建議是:把重複出現的屬性組合(例如標題列的樣式、按鈕的間距與字級)抽成 style,让整个 App 的視覺基調(底色、字型、選單外觀、是否顯示標題列)交給 theme。這樣在換機型、換解析度或做深色模式時,只需改一層定義即可。

本文轉自:blog.csdn.net/zhqingyun163

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