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

白天/夜間模式流暢切換:一套用屬性替換做到的三行接入方案

編輯:關於 Android 開發

這個庫想解決的問題很單純:讓 App 在日間與夜間主題之間切換時,畫面上的背景色、文字色能即時重繪,而不是重啟 Activity 才生效。核心思路是把顏色抽成自訂屬性(attr),再透過主題切換更換屬性對應的值。

版面配置中的用法

// 背景色
android:background="?attr/zzbackground"
app:backgroundAttr="zzbackground"      // 需要立即刷新時,傳入屬性名稱即可(例如 R.attr.zzbackground 就寫 zzbackground)

// 文字色
android:textColor="?attr/zztextColor"
app:textColorAttr="zztextColor"        // 需要立即刷新時同上

Java 端的呼叫方式

@Override
protected void onCreate(Bundle savedInstanceState) {
    // 需要立即套用切換效果的頁面這樣寫
    ChangeModeController.getInstance()
            .init(this, R.attr.class)
            .setTheme(this, R.style.DayTheme, R.style.NightTheme);

    // 其餘頁面只需這樣寫
    // ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 若要額外納入夜間管理的 View
    // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
    // ChangeModeController.getInstance().addBackgroundDrawable(view, R.attr.colorAccent);
    // ChangeModeController.getInstance().addTextColor(view, R.attr.colorAccent);

    // 切換主題
    // ChangeModeController.changeDay(this, R.style.DayTheme);
    // ChangeModeController.changeNight(this, R.style.NightTheme);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    ChangeModeController.onDestory();   // 記得在 onDestroy 回收
}

詳細實作步驟

第一步:自訂屬性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="zzbackground" format="color|reference"/>
    <attr name="zzbackgroundDrawable" format="reference"/>
    <attr name="zztextColor" format="color"/>
    <attr name="zzItemBackground" format="color"/>
</resources>

第二步:建立日/夜兩套主題

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowNoTitle">true</item>
    </style>

    <!-- 日間模式 -->
    <style name="DayTheme" parent="AppTheme">
        <item name="zzbackground">@color/dayBackground</item>
        <item name="zzbackgroundDrawable">@drawable/ic_launcher</item>
        <item name="zztextColor">@color/dayTextColor</item>
        <item name="zzItemBackground">@color/dayItemBackground</item>
    </style>

    <!-- 夜間模式 -->
    <style name="NightTheme" parent="AppTheme">
        <item name="zzbackground">@color/nightBackground</item>
        <item name="zzbackgroundDrawable">@color/nightBackground</item>
        <item name="zztextColor">@color/nightTextColor</item>
        <item name="zzItemBackground">@color/nightItemBackground</item>
        <item name="colorPrimary">@color/colorPrimaryNight</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
        <item name="colorAccent">@color/colorAccentNight</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

接著為這些屬性準備實際的色值:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="dayBackground">#F2F4F7</color>
    <color name="dayTextColor">#000</color>
    <color name="dayItemBackground">#fff</color>
    <color name="nightItemBackground">#37474F</color>
    <color name="nightBackground">#263238</color>
    <color name="nightTextColor">#fff</color>
</resources>

第三步:在版面配置中套用屬性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?attr/zzbackground"
    app:backgroundAttr="zzbackground"
    tools:context="com.thinkfreely.changemode.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:backgroundAttr="colorPrimary"
            app:titleTextColor="?attr/zztextColor"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:gravity="center"
        android:textColor="?attr/zztextColor"
        app:textColorAttr="zztextColor"
        android:background="?attr/zzItemBackground"
        app:backgroundAttr="zzItemBackground"
        android:padding="10dp"
        android:layout_marginBottom="8dp"
        android:textSize="22sp"
        android:textAllCaps="false"
        android:text="夜間模式切換 by Mr.Zk" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>
</LinearLayout>

三個關鍵屬性別寫錯

若希望目前的頁面在切換主題時立即重繪,就必須補上對應的屬性標記:

textColorAttr:修改文字顏色時設定,例如 app:textColorAttr="zztextColor"。
backgroundAttr:修改背景色或背景圖時設定,例如 app:backgroundAttr="zzbackground"。
backgroundDrawableAttr:同樣用於背景色/背景圖,例如 app:backgroundDrawableAttr="zzbackground"。

第四步:頁面中的 Java 呼叫

@Override
protected void onCreate(Bundle savedInstanceState) {
    // 1. 需要立即切換效果的頁面
    ChangeModeController.getInstance()
            .init(this, R.attr.class)
            .setTheme(this, R.style.DayTheme, R.style.NightTheme);

    // 其他頁面
    // ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 2. 切換日/夜模式
    // ChangeModeController.changeDay(this, R.style.DayTheme);
    // ChangeModeController.changeNight(this, R.style.NightTheme);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // 3. 在 onDestroy 中回收
    ChangeModeController.onDestory();
}

三步驟就能接入。若頁面上有動態新增的 View 也需要納入夜間管理,可以補上:

// 將額外的 View 加入夜間管理
// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
// ChangeModeController.getInstance().addBackgroundDrawable(view, R.attr.colorAccent);
// ChangeModeController.getInstance().addTextColor(view, R.attr.colorAccent);

如果切換時還有非標準定義的屬性需要處理,可以在 changeDay/changeNight 之後取值再自行套用:

TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));

原始碼下載:https://www.codesocang.com/kj/transition/33588.html

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