黑白主题切换

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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值