1:
在 style中添加黑夜主题
<!--白天主题-->
<
style
name=
"AppTheme"
parent=
"Theme.AppCompat.Light.DarkActionBar"
>
<
item
name=
"colorPrimary"
>@color/colorPrimary</
item
>
<
item
name=
"colorPrimaryDark"
>@color/colorPrimaryDark</
item
>
<
item
name=
"colorAccent"
>@color/colorAccent</
item
>
</
style
>
<!--黑夜主题-->
<
style
name=
"AppTheme.Black"
parent=
"Theme.AppCompat"
>
</
style
>
2:添加到res目录下anim
activity_out.xml
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<
set
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
>
<
alpha
android
:duration=
"0"
android
:fromAlpha=
"1.0"
android
:interpolator=
"@android:anim/decelerate_interpolator"
android
:toAlpha=
"0.0"
>
</
alpha
>
</
set
>
activity_in.xml
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<
set
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
>
<
alpha
android
:duration=
"0"
android
:fromAlpha=
"0.0"
android
:interpolator=
"@android:anim/accelerate_interpolator"
android
:toAlpha=
"1.0"
>
</
alpha
>
</
set
>
3:创建Preferences和UiUtils
Preferences
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();
}
}
UiUtils
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
;
}
}
}
主Activity
public class
MainActivity
extends
AppCompatActivity {
private int
theme
=
0
;
@Override
protected void
onCreate(Bundle savedInstanceState) {
//切换主题必须放在onCreate()之前
if
(savedInstanceState ==
null
) {
theme
= UiUtils.
getAppTheme
(MainActivity.
this
);
}
else
{
theme
= savedInstanceState.getInt(
"theme"
);
}
setTheme(
theme
);
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
}
public void
click(View v) {
UiUtils.
switchAppTheme
(MainActivity.
this
);
reload();
}
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);
}
}