三行Android代碼實(shí)現(xiàn)白天夜間模式流暢切換
Usage xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //如果當(dāng)前頁(yè)面要立即刷新,這里傳入屬性名稱 比如R.attr.zzbackground 傳zzbackground即可 android:textColor= ?attr/zztextColor app:textColorAttr= zztextColor //
演示效果
Usage xml
android:background="?attr/zzbackground" app:backgroundAttr="zzbackground"http://如果當(dāng)前頁(yè)面要立即刷新,這里傳入屬性名稱 比如R.attr.zzbackground 傳zzbackground即可 android:textColor="?attr/zztextColor" app:textColorAttr="zztextColor"http://如需立即刷新頁(yè)面效果 同上
java
@Override
protected void onCreate(Bundle savedInstanceState) {
// 在要立即切換效果的頁(yè)面調(diào)用此方法
ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
//在其他頁(yè)面調(diào)用此方法
//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);
// 設(shè)置切換
//ChangeModeController.changeDay(this, R.style.DayTheme);
//ChangeModeController.changeNight(this, R.style.NightTheme);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在onDestroy調(diào)用
ChangeModeController.onDestory();
}
詳細(xì)操作描述
第一步:自定義屬性
<?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"> <!-- Customize your theme here. --> <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>
為相關(guān)屬性設(shè)置對(duì)應(yīng)模式的屬性值:
<?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>
第三步:在布局文件中配置使用對(duì)應(yīng)屬性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://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三個(gè)屬性。如需當(dāng)前頁(yè)面立即刷新,需填加相應(yīng)屬性。

第四步:頁(yè)面調(diào)用java代碼
@Override
protected void onCreate(Bundle savedInstanceState) {
//1. 在要立即切換效果的頁(yè)面調(diào)用此方法
ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
//在其他頁(yè)面調(diào)用此方法
//ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2.設(shè)置切換夜間活日間模式
//ChangeModeController.changeDay(this, R.style.DayTheme);//切換日間模式
//ChangeModeController.changeNight(this, R.style.NightTheme);//切換夜間模式
}
@Override
protected void onDestroy() {
super.onDestroy();
//3. 在onDestroy調(diào)用
ChangeModeController.onDestory();
}
代碼調(diào)用三步,即可開始夜間之旅。
如果頁(yè)面有新創(chuàng)建的視圖要加入夜間模式控制,安卓源碼調(diào)用:
//添加額外view至夜間管理 // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary); //ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent); // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);
如果在改變夜間模式時(shí)有其他非標(biāo)準(zhǔn)定義的屬性時(shí),可在ChangeModeController.changeDay或ChangeModeController.changeNight之后調(diào)用如下代碼給相關(guān)屬性賦值:
TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));
源碼下載地址:http://xiazai.jb51.net/201609/yuanma/AndroidChangeMode(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android學(xué)習(xí)教程之動(dòng)態(tài)GridView控件使用(6)
這篇文章主要為大家詳細(xì)介紹了Android動(dòng)態(tài)GridView控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果
這篇文章主要介紹了Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果,具有一定的,感興趣的小伙伴們可以參考一下2016-07-07
Android編程基于自定義View實(shí)現(xiàn)絢麗的圓形進(jìn)度條功能示例
這篇文章主要介紹了Android編程基于自定義View實(shí)現(xiàn)絢麗的圓形進(jìn)度條功能,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義view實(shí)現(xiàn)圓形進(jìn)度條的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解
本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實(shí)現(xiàn)自定義View的構(gòu)造方法,說句人話就是:簡(jiǎn)化自定義View構(gòu)造參數(shù)模板代碼的編寫,實(shí)現(xiàn)自動(dòng)生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App?(Java語(yǔ)言版)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android SQLite事務(wù)處理結(jié)合Listview列表顯示功能示例
這篇文章主要介紹了Android SQLite事務(wù)處理結(jié)合Listview列表顯示功能,較為詳細(xì)的分析了Android使用sqlite數(shù)據(jù)庫(kù)進(jìn)行事務(wù)操作并結(jié)合Listview進(jìn)行列表顯示的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android 實(shí)現(xiàn)桌面未讀角標(biāo)
本文主要介紹了Android實(shí)現(xiàn)桌面未讀角標(biāo)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04

