1:在build.gradle里面引入Support Library包:
compile('com.android.support:appcompat-v7:26.1.0') { exclude module: 'support-v4'}
2:在styles文件里面自定義白天和夜晚兩種主題,
首先 新建兩個(gè)文件夾,分別為:values-night和drawable-night,目錄結(jié)構(gòu)如下:

2.1:白天主題,在res/values/styles文件里面,例如:
<style name="CarnocTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
<style name="CarnocTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
2.2:夜晚主題,在res/values-night/styles定義,例如:
<style name="CarnocTheme" parent="Theme.AppCompat.DayNight">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
此處的顏色均在color.xml文件定義的,因?yàn)橛袃煞N顯示方式,所以可能存在日、夜兩種主題下,兩份顏色定義,具體看第三步介紹。此處只是實(shí)例,可不必粘貼在代碼里面,在第三步統(tǒng)一操作;
<color name="primary">#2196F3</color>
<color name="primary_dark">#1976D2</color>
<color name="accent">@color/primary</color>
三種顏色代表的意思如下圖所示:

3:定義顏色、圖片、點(diǎn)擊效果等兩種效果,一個(gè)為夜間模式下,另一個(gè)為正常模式下的;
3.1:顏色,普通模式下的顏色和通常顏色定義一樣,定義在res/values/colors.xml文件里面,如下:
<color name="primary">#2196F3</color>
<color name="primary_dark">#1976D2</color>
<color name="accent">@color/primary</color>
<color name="text_color_topleft">#333333</color>
<color name="text_color_topcenter">#4e4e4e</color>
<color name="line_color_top">#d8d8d8</color>
<color name="text_color_noimg">#aaaaaa</color>
<color name="listview_item_color1">#FFFFFF</color>
<color name="listview_item_color2">#eeeeee</color>
夜間模式下的顏色定義在res/values-night/color.xml文件里面,例如:
<color name="primary">#2196F3</color>
<color name="primary_dark">#191A1E</color>
<color name="accent">@color/primary</color>
<color name="frament_usercenter_bgcolor">#191A1E</color>
<color name="topbar_bg">#191A1E</color>
<color name="text_color_topleft">#8E8F91</color>
<color name="text_color_topcenter">#8E8F91</color>
<color name="line_color_top">#07080A</color>
<color name="listview_item_color1">#07080A</color>
<color name="listview_item_color2">#191A1E</color>
由于前三個(gè)顏色在主題里面有用到,必須定義,name也必須跟之前的一模一樣,顏色可以根據(jù)項(xiàng)目需求對顏色進(jìn)行更改;其他的顏色,按照項(xiàng)目實(shí)際要求選擇行定義,name和顏色都可以自定義的;
3.2:圖片,圖片很簡單,只要將頁面模式下的圖片放到drawable-night文件夾下即可,注意必須和普通模式下的圖片同名;
3.3:對于點(diǎn)擊事件,由于夜間模式下,點(diǎn)擊效果展示的顏色也和普通模式下有區(qū)別,所以需要定義兩份,將普通的點(diǎn)擊效果文件復(fù)制到drawable-night文件夾下,例如我有一份普通模式下的文件點(diǎn)擊效果,listview_item_selector.xml,代碼如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/listview_item_color1" android:state_pressed="false"/>
<item android:drawable="@color/listview_item_color2" android:state_pressed="true"/>
</selector>
無論是drawable/listview_item_selector.xml文件還是drawable-night/listview_item_selector.xml 代碼、文件名都是一樣的,但是具體里面的item定義文件的顏色,需要在res/values/colors.xml以及res/values-night/color.xml定義兩份.具體代碼,已經(jīng)在3.1里面講過了。
到這里,基本的資源文件定義,主題定義,已經(jīng)結(jié)束了,接下來就是如何應(yīng)用到項(xiàng)目里面:
4:應(yīng)用定義的資源、主題到項(xiàng)目
4.1:AndroidManifest.xml文件應(yīng)用主題,直接在<application>里面進(jìn)行屬性配置即可:
<application
......
android:theme="@style/CarnocTheme.NoActionBar"
.........
>
..........
..........
</application>
4.2:在application的onCreat()方法里面初始化主題,這里的CacheUINightMode只是一個(gè)本地自定義的存儲文件SharedPreferences,用來緩存用戶之前選擇的主題,true代表夜間;
if (CacheUINightMode.getData(this)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
具體設(shè)置頁面,按鈕在點(diǎn)擊切換模式時(shí),代碼如下,已解決卡頓問題:
slipBtnNight.setOnCheckedChangeListener(new SwitchView.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(boolean isChecked)
{
if (isChecked && !CacheUINightMode.getData(UserCenter_SettingActivity.this))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
CacheUINightMode.saveData(UserCenter_SettingActivity.this, true);
startActivity(new Intent(UserCenter_SettingActivity.this,UserCenter_SettingActivity.class));
overridePendingTransition(R.anim.anim_animo_alph_open,R.anim.anim_animo_alph_close);
finish();
}
else if (CacheUINightMode.getData(UserCenter_SettingActivity.this) && !isChecked)
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
CacheUINightMode.saveData(UserCenter_SettingActivity.this, false);
startActivity(new Intent(UserCenter_SettingActivity.this,UserCenter_SettingActivity.class));
overridePendingTransition(R.anim.anim_animo_alph_open,R.anim.anim_animo_alph_close);
finish();
}
}
});
動畫效果代碼如下:
anim_animo_alph_close.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="800"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>
anim_animo_alph_open.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="800"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>
到這就徹底結(jié)束了,啦啦啦啦。。。第一次寫技術(shù)文章,好緊張。。。。