通过EventBus更换android app主题

在设置界面切换主题,是许多app必备的功能,可以在style.xml文件中写好样式,然后在代码中使用setTheme()可以动态切换整个application的主题。具体步骤:
1、准备好要切换的style
style.xml文件:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    <!-- 白天模式-->
    <style name="BaseAppThemeDay" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/colorPrimaryDay</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkDay</item>
        <item name="colorAccent">@color/colorAccentDay</item>
        <item name="statusBarColor">@color/statusBarColorDay</item>
        <item name="android:windowBackground">@color/windowBackgroundDay</item>
        <item name="android:textColorPrimary">@color/textColorPrimaryDay</item>

    </style>

    <!-- 夜间模式-->
    <style name="BaseAppThemeNight" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/colorPrimaryNight</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
        <item name="colorAccent">@color/colorAccentNight</item>
        <item name="android:textColorPrimary">@color/textColorPrimaryNight</item>
        <item name="statusBarColor">@color/statusBarColorNight</item>
        <item name="android:windowBackground">@color/windowBackgroundNight</item>

    </style>

</resources>

style里面的属性,是根据这个图
colors.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimaryDay">#4876FF</color>
    <color name="colorAccentDay">#ffffff</color>
    <color name="colorPrimaryDarkDay">#3A5FCD</color>
    <color name="textColorPrimaryDay">#00ff00</color>
    <color name="statusBarColorDay">#3A5FCD</color>
    <color name="windowBackgroundDay">#888888</color>

    <color name="colorPrimaryNight">#000000</color>
    <color name="colorPrimaryDarkNight">#3C3C3C</color>
    <color name="colorAccentNight">#4876FF</color>
    <color name="textColorPrimaryNight">#00ff00</color>
    <color name="statusBarColorNight">#3C3C3C</color>
    <color name="windowBackgroundNight">#3C3C3C</color>


</resources>

说明:这里是最普通的两套样式,白天模式和夜间模式,当然了,颜色是我自己乱写的,比较丑。。。

2、写一个BaseActivity,application中的所有activity都继承这个activity,这样,修改整个application主题的操作setTheme()放到这个activity实现,每个activity启动时,在onCreate()方法里只需要调用super.onCreate()方法即可同时修改样式。
BaseActivity可以这样写,这里不用为它设置布局文件,因为它根本不需要呈现出来,我们只是为了扩展它,使用theme而已,注意,如果有布局文件的activity需要设置样式,setTheme()方法必须放在setContentView()方法之前,否则样式设置无效

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences preferences = getSharedPreferences("style",MODE_PRIVATE);
        int themeId = preferences.getInt("theme",0);
        if (themeId == 1){
            setTheme(R.style.BaseAppThemeDay);
        }else if (themeId == 2){
            setTheme(R.style.BaseAppThemeNight);
        }
    }
}

3、在设置界面SettingActivity设置app主题,同时将主题添加一个标记,存储到SharedPreferences里面,然后BaseActivity每次判断标记设置对应的主题就OK了,setTheme()还有一个需要注意的地方就是activity必须使用recreate()重启才能生效,那么在设置界面设置好主题后怎样通知用户之前浏览的界面重启呢,我采用了EventBus,具体使用方法,见http://blog.csdn.net/harvic880925/article/details/40660137,很感谢作者,这里我就不再赘述,因为作者讲的很好了,反而我还有很多不清楚的,还得继续学习。
新建一个事件类,我觉得主要就是事件订阅和发布的桥梁,我是这样写的,只有一个方法getMsg():

public class ChangeEvent {
    private String msg;
    public ChangeEvent(String msg){
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }
}

SettingActivity的核心代码:

 public void onClick(View v) {
        preferences = getSharedPreferences("style",MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        switch (v.getId()){
            case R.id.day:
                setTheme(R.style.BaseAppThemeDay);
                editor.putInt("theme", 1);
                editor.commit();

                SettingActivity.this.finish();
                EventBus.getDefault().post(new ChangeEvent("change"));

                break;
            case R.id.night:
                setTheme(R.style.BaseAppThemeNight);
                editor.putInt("theme", 2);
                editor.commit();

                SettingActivity.this.finish();
                EventBus.getDefault().post(new ChangeEvent("change"));
                break;
        }
    }

3、在需要跳转的界面首先在onCreate()方法里注册EventBus,在onDestory()方法里注销,同时重写onEventMainThread()方法:

 //注册EventBus
 EventBus.getDefault().register(this);
 EventBus.getDefault().unregister(this);//注销
 public void onEventMainThread(ChangeEvent event){
        String msg = event.getMsg();
        if (msg.equals("change")){
            this.recreate();
        }
    }

这样如果在设置界面设置了主题,返回之前界面的时候,activity经过重启,主题生效。效果图如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

好了,就写到这里吧,感觉自己太差劲了,继续学习,fighting~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值