1.在styles.xml 中复制夜间模式风格;
<!--黑夜主题-->
<style name="AppTheme.Black" parent="Theme.AppCompat">
</style>
2.将anim文件夹直接复制到res目录下;(在res下创建anim文件夹并将res下的布局复制到anim下)
3.将Preferences 和 UiUtils 和 Night_styleutils 三个工具类复制到工具包中;
4.在每一个 Activity中声明变量
private int theme = 0;
并在super.onCreate(savedInstanceState);方法上面调用 Night_styleutils 方法 代码如下:
Night_styleutils.changeStyle(this, theme, savedInstanceState);
6.在MainActivity 中 复制方法
public void reload() {
Intent intent = getIntent();
overridePendingTransition(R.anim.activity_in, R.anim.activity_out);//进入动画
finish();
overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
startActivity(intent);
}
7.在夜间模式的点击事件中复制如下代码就可以了:
UiUtils.switchAppTheme(getActivity());
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.reload();
以下是一些具体一些内容
anim文件夹内容:
分别创activity_in和activity_out 文件
activity_in:
<?xml version="1.0" encoding="UTF-8"?>
-<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:toAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:duration="0"> </alpha>
</set>
activity_out
<?xml version="1.0" encoding="UTF-8"?>
-<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:toAlpha="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:fromAlpha="1.0" android:duration="0"> </alpha>
</set>
三个工具类内容
package utils;
import android.content.Context;
import android.os.Bundle;
import com.gyj.jrtt_project.activitis.MainActivity;
public class Night_styleutils {
private Context context;
private Bundle savedInstanceState;
public static void changeStyle(Context context, int theme, Bundle savedInstanceState) {
//切换主题必须放在onCreate()之前
if (savedInstanceState == null) {
theme = UiUtils.getAppTheme(context);
} else {
theme = savedInstanceState.getInt("theme");
}
context.setTheme(theme);
}
}
package com.fantasy.switchtheme;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Fantasy on 2016/3/3.
*/
public class Preferences {
private static final String shared_name="user_guide";
private static SharedPreferences sp;
public static String getString(Context context, String key,
String defaultValues) {
SharedPreferences sp = context.getSharedPreferences(shared_name,
context.MODE_PRIVATE);
return sp.getString(key, defaultValues);
}
public static void setString(Context context, String key, String Values) {
SharedPreferences sp = context.getSharedPreferences(shared_name,
context.MODE_PRIVATE);
sp.edit().putString(key, Values).commit();
}
}
package com.fantasy.switchtheme;
import android.content.Context;
/**
* Created by Fantasy on 2016/4/15.
*/
public class UiUtils {
//获取主题
public static int getAppTheme(Context ctx) {
String value = Preferences.getString(ctx, "activity_theme", "1");
switch (Integer.valueOf(value)) {
case 1:
return R.style.AppTheme;//白色主题
case 2:
return R.style.AppTheme_Black;
default:
return R.style.AppTheme;//默认白色
}
}
//切换主题
//当然也可以使用资源ID来进行标记
public static void switchAppTheme( Context ctx){
String value = Preferences.getString(ctx, "activity_theme", "1");
switch (Integer.valueOf(value)){
case 1:
Preferences.setString(ctx, "activity_theme", "2");
break;
case 2:
Preferences.setString(ctx, "activity_theme", "1");
break;
default:
Preferences.setString(ctx, "activity_theme", "1");
break;
}
}
}