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

Android 主題切換(一):用內建 Style 實作日夜模式

編輯:關於 Android 程式設計

Android 換膚大致有三種主流做法:第一種是透過內建的 style 切換,最常用於夜間/日間模式;第二種是打獨立的 apk 作為插件;第三種是下載 zip 解壓到約定的檔案目錄,執行時再讀入記憶體。本篇先講第一種,也是最輕量的一種。

步驟一:定義自訂屬性

先在 values 下建立 attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="main">
    </declare-styleable>
</resources>

步驟二:在 styles.xml 定義兩套主題

<style name="dayTheme">
    <item name="bgColor">#ffffff</item>
    <item name="buttonBgColor">#80000000</item>
    <item name="buttonTextColor">#80ffffff</item>
    <item name="textSize">14sp</item>
</style>

<style name="nightTheme">
    <item name="bgColor">#cc000000</item>
    <item name="buttonBgColor">#80ffffff</item>
    <item name="buttonTextColor">#80000000</item>
    <item name="textSize">14sp</item>
</style>

兩套主題的屬性名稱完全一致,只是取值不同——這是整個方案能運作的前提。屬性名對應到自訂的 attr 資源,值則由目前生效的主題決定。

步驟三:在布局中使用屬性引用

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/bgColor" >

    <Button
        android:id="@+id/swtichThemeBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="?attr/textSize"
        android:text="按下按鈕切換夜間模式"
        android:layout_margin="10dip"
        android:background="?attr/buttonBgColor"
        android:textColor="?attr/buttonTextColor" />
</LinearLayout>

關鍵語法是 ?attr/xxx。注意問號與 @ 的差別:@ 是直接引用資源,?attr 則是「去目前主題裡查這個屬性的值」,換主題時引用會自動跟著改變。

步驟四:Activity 中設定並切換主題

public class MainActivity extends Activity {

    private Button mSwtichThemeBtn;
    private boolean isNight;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        setTheme((isNight = sp.getBoolean("isNight", false))
                ? R.style.nightTheme : R.style.dayTheme);
        setContentView(R.layout.activity_main);

        mSwtichThemeBtn = (Button) this.findViewById(R.id.swtichThemeBtn);
        mSwtichThemeBtn.setText(isNight ? "切換日間模式" : "切換夜間模式");
        mSwtichThemeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editor edit = sp.edit();
                edit.putBoolean("isNight", !isNight);
                edit.commit();
                recreateForTheme();
            }
        });
    }

    @SuppressLint("NewApi")
    public void recreateForTheme() {
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            this.recreate();
        } else {
            this.finish();
            startActivity(new Intent(MainActivity.this, MainActivity.class));
        }
    }
}

這裡有兩個不可更動的順序:setTheme() 必須在 setContentView() 之前呼叫,否則主題已經套用到 View 上了,設定不生效。另外,切換主題後必須重建 Activity,recreate() 在 API 11 以下不可用,只能手動 finish 再重新啟動自己。

用 SharedPreferences 記錄目前是日模式還是夜模式,是為了讓重建後的 Activity 能記住使用者的選擇,同時也讓下次開啟 App 時直接進入上次的狀態。

這種做法的優點是完全不需要額外資源檔、實作簡單;缺點是配色寫死在 style 裡,無法動態更換圖片資源。如果需要換的是整套圖片(例如換皮膚),就得用第二種 apk 插件方案了。

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