之前写过一篇文章:RxBus的实现及简单使用。今天我们尝试使用RxBus动态切换主题。

一、定义主题颜色

color.xml

    #F44336    #D32F2F    #F44336    #E91E63    #C2185B    #E91E63    #795548    #5D4037    #795548    #2196F3    #1976D2    #2196F3    #607D8B    #455A64    #607D8B    #FFEB3B    #FBC02D    #FFEB3B    #673AB7    #512DA8    #673AB7    #4CAF50    #388E3C    #4CAF50    #FF5722    #E64A19    #FF5722    #9E9E9E    #616161    #9E9E9E    #00BCD4    #0097A7    #00BCD4    #FFC107    #FFA000    #FFC107    #2196F3    #1E88E5    @color/primary

二、定义主题样式

styles.xml

                                               

三、在需要的地方弹出主题选择对话框

private void showThemeChooseDialog() {        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);        builder.setTitle("设置主题");        Integer[] res = new Integer[]{R.drawable.red_round, R.drawable.brown_round, R.drawable.blue_round,                R.drawable.blue_grey_round, R.drawable.yellow_round, R.drawable.deep_purple_round,                R.drawable.pink_round, R.drawable.green_round, R.drawable.deep_orange_round,                R.drawable.grey_round, R.drawable.cyan_round};        List list = Arrays.asList(res);        ColorsListAdapter adapter = new ColorsListAdapter(MainActivity.this, list);        adapter.setCheckItem(MyThemeUtils.getCurrentTheme(MainActivity.this).getIntValue());        GridView gridView = (GridView) LayoutInflater.from(MainActivity.this).inflate(R.layout.colors_panel_layout, null);        gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);        gridView.setCacheColorHint(0);        gridView.setAdapter(adapter);        builder.setView(gridView);        final AlertDialog dialog = builder.show();        gridView.setOnItemClickListener(                new AdapterView.OnItemClickListener() {                    @Override                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                        dialog.dismiss();                        int value = MyThemeUtils.getCurrentTheme(MainActivity.this).getIntValue();                        if (value != position) {                            PreferenceUtils.getInstance(MainActivity.this).saveParam("change_theme_key", position);                            changeTheme(MyThemeUtils.Theme.mapValueToTheme(position));                        }                    }                }        );    }
对话框圆形颜色选项

在drawable下新建red_round.xml,其他主题颜色类似

<?xml version="1.0" encoding="utf-8"?>    

四、ColorsListAdapter、colors_image_layout.xml,主题选择框中颜色适配器及其xml

ColorsListAdapter

public class ColorsListAdapter extends BaseAdapter {    private int checkItem;    Context context;    List list;    public ColorsListAdapter(Context context, List list) {        this.context = context;        this.list = list;    }    @Override    public int getCount() {        return list.size();    }    @Override    public Object getItem(int position) {        return list.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        Holder holder;        if (convertView == null) {            convertView = LayoutInflater.from(context).inflate(R.layout.colors_image_layout, null);            holder = new Holder();            holder.imageView1 = (ImageView) convertView.findViewById(R.id.img_1);            holder.imageView2 = (ImageView) convertView.findViewById(R.id.img_2);            convertView.setTag(holder);        } else {            holder = (Holder) convertView.getTag();        }        holder.imageView1.setImageResource(list.get(position));        if (checkItem == position) {            holder.imageView2.setImageResource(R.drawable.ic_done_white);        }        return convertView;    }    public void setCheckItem(int checkItem) {        this.checkItem = checkItem;    }    static class Holder {        ImageView imageView1;        ImageView imageView2;    }}

colors_image_layout.xml

<?xml version="1.0" encoding="utf-8"?>        

五、使用RxBus发布事件和处理事件

发布

 private void changeTheme(MyThemeUtils.Theme theme) {       RxBus.getInstance().post(new RxbusEvent(theme));    }

接收

rxSbscription=RxBus.getInstance().toObserverable(RxbusEvent.class)                .subscribe(new Action1() {                    @Override                    public void call(RxbusEvent rxbusEvent) {                        changeTheme(rxbusEvent.getTheme());                    }                });

六、用到的工具类

PreferenceUtils

public class PreferenceUtils {    private SharedPreferences sharedPreferences;    private SharedPreferences.Editor shareEditor;    private static PreferenceUtils preferenceUtils = null;    public static final String NOTE_TYPE_KEY = "NOTE_TYPE_KEY";    public static final String EVERNOTE_ACCOUNT_KEY = "EVERNOTE_ACCOUNT_KEY";    public static final String EVERNOTE_NOTEBOOK_GUID_KEY = "EVERNOTE_NOTEBOOK_GUID_KEY";    private PreferenceUtils(Context context){        sharedPreferences = context.getSharedPreferences("ThemeSetting", Context.MODE_PRIVATE);        shareEditor = sharedPreferences.edit();    }    public static PreferenceUtils getInstance(Context context){        if (preferenceUtils == null) {            synchronized (PreferenceUtils.class) {                if (preferenceUtils == null) {                    preferenceUtils = new PreferenceUtils(context.getApplicationContext());                }            }        }        return preferenceUtils;    }    public String getStringParam(String key){        return getStringParam(key, "");    }    public String getStringParam(String key, String defaultString){        return sharedPreferences.getString(key, defaultString);    }    public void saveParam(String key, String value)    {        shareEditor.putString(key,value).commit();    }    public boolean getBooleanParam(String key){        return getBooleanParam(key, false);    }    public boolean getBooleanParam(String key, boolean defaultBool){        return sharedPreferences.getBoolean(key, defaultBool);    }    public void saveParam(String key, boolean value){        shareEditor.putBoolean(key, value).commit();    }    public int getIntParam(String key){        return getIntParam(key, 0);    }    public int getIntParam(String key, int defaultInt){        return sharedPreferences.getInt(key, defaultInt);    }    public void saveParam(String key, int value){        shareEditor.putInt(key, value).commit();    }    public long getLongParam(String key){        return getLongParam(key, 0);    }    public long getLongParam(String key, long defaultInt){        return sharedPreferences.getLong(key, defaultInt);    }    public void saveParam(String key, long value){        shareEditor.putLong(key, value).commit();    }    public void removeKey(String key){        shareEditor.remove(key).commit();    }}

MyThemeUtils

public class MyThemeUtils {    public static void changTheme(Activity activity, Theme theme) {        if (activity == null)            return;        int style = R.style.RedTheme;        switch (theme) {            case BROWN:                style = R.style.BrownTheme;                break;            case BLUE:                style = R.style.BlueTheme;                break;            case BLUE_GREY:                style = R.style.BlueGreyTheme;                break;            case YELLOW:                style = R.style.YellowTheme;                break;            case DEEP_PURPLE:                style = R.style.DeepPurpleTheme;                break;            case PINK:                style = R.style.PinkTheme;                break;            case GREEN:                style = R.style.GreenTheme;                break;            case DEEP_ORANGE:                style = R.style.DeepOrangeTheme;                break;            case GREY:                style = R.style.GreyTheme;                break;            case CYAN:                style = R.style.CyanTheme;                break;            case AMBER:                style = R.style.AmberTheme;                break;            default:                break;        }        activity.setTheme(style);    }    public static Theme getCurrentTheme(Context context) {        int value = PreferenceUtils.getInstance(context)                .getIntParam("change_theme_key", 0);        return MyThemeUtils.Theme.mapValueToTheme(value);    }    public enum Theme {        RED(0),        BROWN(1),        BLUE(2),        BLUE_GREY(3),        YELLOW(4),        DEEP_PURPLE(5),        PINK(6),        GREEN(7),        DEEP_ORANGE(8),        GREY(9),        CYAN(10),        AMBER(11);        private int mValue;        Theme(int value) {            this.mValue = value;        }        public static Theme mapValueToTheme(final int value) {            for (Theme theme : Theme.values()) {                if (value == theme.getIntValue()) {                    return theme;                }            }            // If run here, return default            return RED;        }        static Theme getDefault() {            return RED;        }        public int getIntValue() {            return mValue;        }    }}

基类BaseActivity,主题的初始化。注意,需要变换主题的Activity需继承BaseActivity

public class BaseActivity extends AppCompatActivity {    protected PreferenceUtils preferenceUtils;    @Override    protected void onCreate(Bundle savedInstanceState) {        preferenceUtils = PreferenceUtils.getInstance(this);        initTheme();        super.onCreate(savedInstanceState);    }    private void initTheme() {        MyThemeUtils.Theme theme = MyThemeUtils.getCurrentTheme(this);        MyThemeUtils.changTheme(this, theme);    }}

参考:
https://github.com/lguipeng/Notes

更多相关文章

  1. android ToolBar使用默认返回键,修改颜色
  2. android activity以对话框形式显示
  3. Android——系统提示对话框(AlertDialog)
  4. Android 状态栏和应用标题栏颜色保持一致
  5. android 之Dialog的多种Dialog对话框(以及反射在自定义对话框的
  6. Android改变图片颜色的自定义控件(十)

随机推荐

  1. android studio 主线程中访问网络,遇到and
  2. Android Build获得系统信息
  3. Android基础入门教程——2.3.1 TextView(
  4. Android GoogleMap的放大缩小地图控件的
  5. android Frame动画概述+示例
  6. android button light 流程分析(二) — H
  7. 开源项目之Android(安卓)推送框架(androi
  8. AndTask v2.2 安安任务管理器 发布
  9. android: 大量开源项目源代码分享
  10. Android 得到连接热点的ip的方法