編輯:關於 Android 程式設計
Usage xml:android:background= ?attr/zzbackground,app:backgroundAttr= zzbackground //如果當前頁面要立即刷新,這裡傳入屬性名稱,比如 R.attr.zzbackground 傳 zzbackground 即可;android:textColor= ?attr/zztextColor,app:textColorAttr= zztextColor //...
演示效果

Usage xml
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(); // 在onDestroy調用 ChangeModeController.onDestory(); }
詳細操作描述
第一步:自定義屬性
<?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>
第二步:配置夜間 style 檔案
<resources> <!-- Base application theme. --> <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" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="https://schemas.android.com/apk/res-auto" 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、backgroundAttr、backgroundDrawableAttr 三個屬性。若需當前頁面立即刷新,需加上相應屬性。

第四步:頁面呼叫 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至夜間管理 // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary); //ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent); // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);
若在切換夜間模式時有其他非標準定義的屬性,可在 ChangeModeController.changeDay 或 ChangeModeController.changeNight 之後呼叫以下程式碼為相關屬性賦值:
TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));
源碼下載地址:https://xiazai.jb51.net/201609/yuanma/AndroidChangeMode(jb51.net).rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android Studio 中文變成豆腐方塊?改一個字型設定就解決
剛裝好 Android Studio,打開一看:介面上的中文、LogCat 裡的中文輸出,全都變成一個個空心的小方框。程式碼沒問題、編碼也沒問題,問題出在 IDE 找不到
仿百度地圖街景效果實現:MapView 與 PanoView 的聯動
用過百度地圖的同學應該都熟悉街景功能——能看到不少地方的實景畫面。本文就以街景效果的實現為線索,走一遍完整流程。 百度在開放平台為開發者準備了豐富的 SDK 與文檔。關於
Android 事件攔截機制入門:從模擬公司組織理解 Touch 事件
前言 事件攔截機制是 Android 初學者常感到困惑的主題。其實只要理解事件如何從上層容器往下傳,以及何時被攔截、何時被消費,就能掌握大部分 Touch 事件流程。 什
Android ImageView 寬度填滿且高度按比例伸縮
在 ListView 或一般版面中,常會遇到圖片寬度要填滿容器,但高度又要依原始比例自動伸縮的需求。Android 的 ImageView 內建 scaleType 不一