这是系统自带的一个夜间模式,只需要四步即可实现
1、添加values-night资源文件夹,拷贝values下的colors.xml到values-night下,修改色值,及其style.xml更改为Night-NoActionBar
values下的colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="jinse">#FFD700</color>
<color name="blue">#0000CD</color>
<color name="tianlan">#F0FFFF</color>
</resources>
values-night下的colors.xml
<color name="colorPrimary">#000000</color>
<color name="colorPrimaryDark">#D3D3D3</color>
<color name="colorAccent">#DCDCDC</color>
<color name="jinse">#C0C0C0</color>
<color name="blue">#DCDCDC</color>
<color name="tianlan">#696969</color>
2、创建Application类
public class App extends Application {
private static Application mApp;
public static int mMode;
@Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
mApp=this;
}
public Application getApp() {
return mApp;
}
public static App getInstance(){
return (App) mApp;
}
private void setDayNightMode() {
//根据sp里面的模式设置对应的日夜间模式 y:2;no:1
mMode = (int) SpUtil.getParam(Constants.MODE, AppCompatDelegate.MODE_NIGHT_NO);
//Log.d(TAG, "setDayNightMode: "+mMode);
UIModeUtil.setAppMode(mMode);
}
}
3、XML写一个switch控件来实现切换
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/tianlan"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/jinse"
/>
<Switch
android:id="@+id/is_click"
android:layout_below="@+id/btn_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
4、具体代码逻辑在Activity中实现
private void initLinster() {
mIsClick.setChecked (false);
mIsClick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//这里必须为isPressed,不然会黑屏
if (compoundButton.isPressed()){
MainActivity.mType = "switch_type";
SpUtil.setParam (Constants.CURRENT_FRAG_TYPE, MainActivity.TYPE_SETTTINGS);
UIModeUtil.changeModeUI (MainActivity.this);
}
}
});
需要用到的工具类
夜间模式工具类
/**
* 使用这个类需要把style设置成日夜间的样式:Theme.AppCompat.DayNight.NoActionBar
*/
public class UIModeUtil {
/**
* 夜间模式切换,切换选项时调用
*/
public static void changeModeUI(AppCompatActivity activity){
//获取当前Acitivity的日夜间的模式
int currentNightMode = activity.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
if(currentNightMode == Configuration.UI_MODE_NIGHT_NO){
//日间模式,切成夜间模式
activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
SpUtil.setParam(Constants.MODE,AppCompatDelegate.MODE_NIGHT_YES);
App.mMode = AppCompatDelegate.MODE_NIGHT_YES;
}else{
activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
SpUtil.setParam(Constants.MODE,AppCompatDelegate.MODE_NIGHT_NO);
App.mMode = AppCompatDelegate.MODE_NIGHT_NO;
}
}
/**
* 设置当前的模式
* @param activity
*/
public static void setActModeFromSp(AppCompatActivity activity){
int mode = (int) SpUtil.getParam(Constants.MODE, AppCompatDelegate.MODE_NIGHT_NO);
activity.getDelegate().setLocalNightMode(mode);
}
/**
* 设置当前的模式
* @param activity
*/
public static void setActModeUseMode(AppCompatActivity activity,int mode){
activity.getDelegate().setLocalNightMode(mode);
}
public static void setAppMode(int mode){
//给整个应用设置模式
AppCompatDelegate.setDefaultNightMode(mode);
}
}
Sharepreference工具类
public class SpUtil {
/**
* 保存在手机里面的文件名
*/
private static final String FILE_NAME = "share_date";
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
* @param key
* @param object
*/
public static void setParam(String key, Object object){
String type = object.getClass().getSimpleName();
SharedPreferences sp = App.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if("String".equals(type)){
editor.putString(key, (String)object);
}
else if("Integer".equals(type)){
editor.putInt(key, (Integer)object);
}
else if("Boolean".equals(type)){
editor.putBoolean(key, (Boolean)object);
}
else if("Float".equals(type)){
editor.putFloat(key, (Float)object);
}
else if("Long".equals(type)){
editor.putLong(key, (Long)object);
}
editor.commit();
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
* @param key
* @param defaultObject
* @return
*/
public static Object getParam(String key, Object defaultObject){
String type = defaultObject.getClass().getSimpleName();
SharedPreferences sp = App.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
if("String".equals(type)){
return sp.getString(key, (String)defaultObject);
}
else if("Integer".equals(type)){
return sp.getInt(key, (Integer)defaultObject);
}
else if("Boolean".equals(type)){
return sp.getBoolean(key, (Boolean)defaultObject);
}
else if("Float".equals(type)){
return sp.getFloat(key, (Float)defaultObject);
}
else if("Long".equals(type)){
return sp.getLong(key, (Long)defaultObject);
}
return null;
}
}
Constants环境变量
public interface Constants {
boolean isDebug = true;
String PATH_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + "code" + File.separator + "GeekNews";
String FILE_PROVIDER_AUTHORITY = "com.baidu.geek.fileprovider";
String MODE = "day_night_mode";
String CURRENT_FRAG_TYPE = "current_frag_type";
}
好了,加完这些,就可以实现夜间模式了,大家可以去试一试,不过这个方法有点不好的就是,不够平滑,会黑屏一瞬间,我也会写一篇不黑屏实现夜间模式的文章,大家请之后留意
文章很短,路还漫长,大家好,我是玖玖君,一个帅气与才华并存的男人,我们下期再见。
网友评论