在support 23.2中增加了日夜间主题:
详细应用:
第一步:在application 中设置默认主题
<pre name="code" class="java">package com.xuan.theme;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatDelegate;
/**
* @author xuanyouwu
* @email xuanyouwu@163.com
* @time 2016-03-09 09:44
*/
public class BaseApplication extends Application {
public static SharedPreferences sp;
public static final String THEMID = "themId";
@Override
public void onCreate() {
super.onCreate();
sp = getSharedPreferences("sp", Context.MODE_PRIVATE);
if (sp.getInt(THEMID, 1) == 2) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
第二步:夜间模式:
第三步:
设置夜间模式:
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
设置日间模式:
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
效果图:
源码:在群里:476899320