Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂樣式與主題入門:style 加在 View,theme 加在 Application

編輯:關於 Android 開發

Android 自訂樣式與主題入門:style 加在 View,theme 加在 Application


這篇做一個簡單練習,釐清 style 與 theme 最基本的差別:樣式(style)是加在個別 View 上,主題(theme)則是加在 Application 或 Activity 上。

先看 styles.xml。這裡示範了樣式的繼承——MyFontSon 用 parent 指向 MyFont,只覆寫字級大小,其餘屬性自動沿用:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="https://schemas.android.com/apk/res/android">

    <!-- 自定義樣式 -->
    <style name="MyFont">
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">40sp</item>
    </style>

    <style name="MyFontSon" parent="@style/MyFont">
        <item name="android:textSize">30sp</item>
    </style>

    <!-- 自定義主題 -->
    <style name="MyTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">#ffffff</item>
    </style>
</resources>

布局裡放兩個 TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="樣式和主題" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="樣式和主題" />
</LinearLayout>

主題則在 Manifest 的 application 節點上套用:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="ss"
    android:theme="@style/MyTheme" >

簡單記憶:style 是一組屬性的集合,用 View 的 style 屬性引用;theme 本質上也是 style,只是套用在 Application 或 Activity 這種帶有「視窗」概念的元件上,會影響整個畫面的外觀,例如隱藏標題列、設定全域背景色等。

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