为了简单在这里我们使用继承自Theme.AppCompat.Light.DarkActionBar的主题样式来代替日间模式。
作者:@JohnTsai
本文为作者原创,转载请注明出处:https://www.cnblogs.com/JohnTsai/p/4550144.html
详情看原贴 ㊣↑↑↑↑↑↑
简单应用:
styles.xml:
<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>
</style>
<!--夜间-->
<style name="AppThemeDark" parent="Theme.AppCompat">
</style>
</resources>
Utils 工具类:
public class Utils {
// 保存主题到本地
public static class SharedPreferrenceHelper {
private static final String THEME = "theme";
public static void settheme(Context context, String theme){
SharedPreferences sp = context.getSharedPreferences("demo",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(THEME,theme);
editor.apply();
}
public static String gettheme(Context context){
SharedPreferences sp = context.getSharedPreferences("demo",Context.MODE_PRIVATE);
return sp.getString(THEME,"1");
}
}
// 获得应用主题
public static int getAppTheme(Context context){
String value = SharedPreferrenceHelper.gettheme(context);
switch (Integer.valueOf(value)){
case 1:
return R.style.AppTheme;
case 2:
return R.style.AppThemeDark;
default:
return R.style.AppTheme;
}
}
// 切换主题
public static void switchAppTheme(Context context){
String value = SharedPreferrenceHelper.gettheme(context);
switch (Integer.valueOf(value)){
case 1:
SharedPreferrenceHelper.settheme(context,"2");
break;
case 2:
SharedPreferrenceHelper.settheme(context,"1");
break;
default:
SharedPreferrenceHelper.settheme(context,"1");
break;
}
}
}
Main界面:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private int theme = 0;
/**
* 切换
*/
private Button mBtn;
// 判断当前主题
@Override
protected void onResume() {
super.onResume();
if (theme == Utils.getAppTheme(this)) {
} else {
reload();
}
}
// 存储主题状态
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("theme", theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// 选择主题状态
if (savedInstanceState == null) {
theme = Utils.getAppTheme(this);
} else {
theme = savedInstanceState.getInt("theme");
}
// 设置主题
setTheme(theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
// 重新生成activity,切换主题
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);//不设置进入退出动画
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
private void initView() {
mBtn = (Button) findViewById(R.id.btn);
mBtn.setOnClickListener(this);
}
// 点击事件,切换主题
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btn:
Utils.switchAppTheme(this);
reload();
break;
}
}
}
解决闪屏问题-加入切换动画:
anim文件:—–
start_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
out_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
// 加入动画
// 重新生成activity,切换主题
public void reload() {
Intent intent = getIntent();
//设置进入动画
overridePendingTransition(R.anim.start_anim, R.anim.out_anim);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
//设置进入动画
overridePendingTransition(R.anim.start_anim, R.anim.out_anim);
startActivity(intent);
}