編輯:關於 Android 程式設計
Android 允許開發者自訂主題與樣式。當同一組屬性(寬高、字級、字色、背景……)在多個地方重複出現時,就該把它抽出來變成 Style,讓版面檔保持乾淨,日後改動也只需改一處。
在 res/values/ 底下新增一個 styles.xml,以 <resources> 為根節點,內部放 <item>。item 的 name 就是屬性名,內容就是屬性值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyText" parent="@android:style/TextAppearance">
<item name="android:textColor">#987456</item>
<item name="android:textSize">24sp</item>
</style>
</resources>
這裡的 parent 屬性很關鍵:它宣告這個 Style 繼承自哪一個既有 Style。繼承而來的所有屬性都會被帶進來,而且你可以針對其中任何一項覆寫成自己想要的值——不必從零開始。
寫好之後,在版面檔裡直接引用即可。以下用一個 EditText 示範:
<?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">
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/MyText"
android:text="測試一下下" />
</LinearLayout>
Theme 與 Style 本質相同,差別只在套用範圍:Style 作用在單一 View 上,Theme 則作用在整個 Application 或 Activity 上。所以凡是涉及視窗層級的設定(標題列、視窗邊框、背景),都必須用 Theme。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyText" parent="@android:style/TextAppearance">
<item name="android:textColor">#987456</item>
<item name="android:textSize">24sp</item>
</style>
<style parent="@android:style/Theme" name="CustomTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">@drawable/icon</item>
<item name="android:windowBackground">?android:windowFrame</item>
</style>
</resources>
這個 CustomTheme 繼承自系統預設的 Theme,並覆寫了三個屬性。第三個屬性的寫法要特別注意:?android:windowFrame 前面那個問號代表「引用同一主題中某個 item 的值」,也就是取用上面定義的 icon 圖片。用 @ 是直接指向資源,用 ? 是取值於目前主題中的屬性,兩者意義不同,寫錯就會找不到資源。
於 AndroidManifest.xml 的 <application> 加上 android:theme,整個 App 的外觀就換了:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/CustomTheme">
<activity
android:name=".TestStyle"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
套用之後,標題列消失,視窗邊框與背景都換成我們指定的圖片。若只想讓某幾個 Activity 長得不一樣,把 android:theme 移到該 <activity> 標籤底下即可覆寫全域設定。
Theme 也可以在 Java 裡指定,但有一個順序上的硬性要求——必須在 setContentView() 之前呼叫 setTheme(),否則畫面已經配置完成,主題不會生效。
package com.test.shang;
import android.app.Activity;
import android.os.Bundle;
public class TestStyle extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.CustomTheme); // 一定要在 setContentView 之前
setContentView(R.layout.test_style);
}
}
優先在 Manifest 裡宣告 Theme,讓全域外觀一致;只有特殊頁面才用程式碼覆寫。至於 Style,則盡量把重複屬性抽成命名樣式,特別是清單項與按鈕這類大量重複使用的元件,日後改設計稿時會省下大量時間。
《Android 從入門到精通》第 17 篇:ProgressBar 進度條完全解析
凡是需要「讓使用者等一等」的場景,幾乎都離不開進度條。本文延續《Android 從入門到精通》的系列進度,把 ProgressBar 的類別結構、常用方法與一個可直接跑起
Android 藍牙掃描設備機制解析:從 hcitool 到 Framework 與 BlueZ
本文以 Android 4.1 與 BlueZ 為例,整理藍牙設備掃描的常見路徑。掃描可以透過 Linux 命令列工具 hcitool 直接觸發,也可以由 Android
Android 端小米推送實戰:從 Demo 拆解到移植上線
做 Android 專案時,消息推送幾乎是標配。市面上可行的方案不少:C2DM、輪詢、簡訊喚醒、MQTT 協議、XMPP 協議,以及各家的第三方平台。綜合評估當時專案的需
Android 模擬器無法輸入中文?AVD 中文輸入法設定教學
在 Android 開發與測試過程中,Eclipse、Android Studio 內建模擬器,或 Genymotion 這類第三方模擬器,常被用來快速驗證應用功能。然而