日夜切换两种模式

来源:互联网 发布:大学生炒股知乎 编辑:程序博客网 时间:2024/04/19 12:28

第一种:

1:在values文件夹下的colors.xml里添加:

<color name="textColor">#3b3b3b</color><color name="textColor_night">#FFFFFF</color><color name="backgroundColor">#FFFFFF</color><color name="backgroundColor_night">#3b3b3b</color>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2:创建Theme管理类

package com.example.administrator.demo.view;import android.content.Context;import android.content.res.Resources;import java.util.HashMap;import java.util.LinkedList;import java.util.List;public class ThemeManager {    // 默认是日间模式    private static ThemeMode mThemeMode = ThemeMode.DAY;    // 主题模式监听器    private static List<OnThemeChangeListener> mThemeChangeListenerList = new LinkedList<>();    // 夜间资源的缓存,key : 资源类型, 值<key:资源名称, value:int值>    private static HashMap<String, HashMap<String, Integer>> sCachedNightResrouces = new HashMap<>();    // 夜间模式资源的后缀,比如日件模式资源名为:R.color.activity_bg, 那么夜间模式就为 :R.color.activity_bg_night    private static final String RESOURCE_SUFFIX = "_night";    /**     * 主题模式,分为日间模式和夜间模式     */    public enum ThemeMode {        DAY, NIGHT    }    /**     * 设置主题模式     */    public static void setThemeMode(ThemeMode themeMode) {        if (mThemeMode != themeMode) {            mThemeMode = themeMode;            if (mThemeChangeListenerList.size() > 0) {                for (OnThemeChangeListener listener : mThemeChangeListenerList) {                    listener.onThemeChanged();                }            }        }    }    /**     * 根据传入的日间模式的resId得到相应主题的resId,注意:必须是日间模式的resId     * 日间模式的resId     * 相应主题的resId,若为日间模式,则得到dayResId;反之夜间模式得到nightResId     */    public static int getCurrentThemeRes(Context context, int dayResId) {        if (getThemeMode() == ThemeMode.DAY) {            return dayResId;        }        // 资源名        String entryName = context.getResources().getResourceEntryName(dayResId);        // 资源类型        String typeName = context.getResources().getResourceTypeName(dayResId);        HashMap<String, Integer> cachedRes = sCachedNightResrouces.get(typeName);        // 先从缓存中去取,如果有直接返回该id        if (cachedRes == null) {            cachedRes = new HashMap<>();        }        Integer resId = cachedRes.get(entryName + RESOURCE_SUFFIX);        if (resId != null && resId != 0) {            return resId;        } else {            //如果缓存中没有再根据资源id去动态获取            try {                // 通过资源名,资源类型,包名得到资源int值                int nightResId = context.getResources().getIdentifier(entryName + RESOURCE_SUFFIX, typeName, context.getPackageName());                // 放入缓存中                cachedRes.put(entryName + RESOURCE_SUFFIX, nightResId);                sCachedNightResrouces.put(typeName, cachedRes);                return nightResId;            } catch (Resources.NotFoundException e) {                e.printStackTrace();            }        }        return 0;    }    /**     * 注册ThemeChangeListener     */    public static void registerThemeChangeListener(OnThemeChangeListener listener) {        if (!mThemeChangeListenerList.contains(listener)) {            mThemeChangeListenerList.add(listener);        }    }    /**     * 反注册ThemeChangeListener     */    public static void unregisterThemeChangeListener(OnThemeChangeListener listener) {        if (mThemeChangeListenerList.contains(listener)) {            mThemeChangeListenerList.remove(listener);        }    }    /**     * 得到主题模式     */    public static ThemeMode getThemeMode() {        return mThemeMode;    }    /**     * 主题模式切换监听器     */    public interface OnThemeChangeListener {        /**         * 主题切换时回调         */        void onThemeChanged();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

3:Activity中使用

package com.example.administrator.demo.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import com.example.administrator.demo.R;import com.example.administrator.demo.view.ThemeManager;public class MainActivity extends Activity implements ThemeManager.OnThemeChangeListener{    private Button day_night_mode;    private RelativeLayout relativeLayout;    private boolean isDay = true;//默认是日间模式    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ThemeManager.registerThemeChangeListener(this);        day_night_mode = (Button) findViewById(R.id.day_night_mode);        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);        day_night_mode.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(isDay==true){                    ThemeManager.setThemeMode(ThemeManager.ThemeMode.NIGHT );                    day_night_mode.setText("日间模式");                    isDay=false;                }else {                    ThemeManager.setThemeMode(ThemeManager.ThemeMode.DAY );                    day_night_mode.setText("夜间模式");                    isDay=true;                }            }        });    }    @Override    public void onThemeChanged() {        //日间模式下的颜色        day_night_mode.setTextColor(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.textColor)));        relativeLayout.setBackgroundColor(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.backgroundColor)));    }    @Override    protected void onDestroy() {        super.onDestroy();        ThemeManager.unregisterThemeChangeListener(this);    }}第二种:1在res下创建一个values-night文件夹,将values中的colors粘贴过来,将里面的颜色改成黑色2在values文件夹下的styles,将里面的
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

中的Light改成DayNight,日夜间模式
3:在main类中日夜间切换的点击事件中切换,并通过sharedPreferences进行保存状态。
//点击事件public void button(View view){    SharedPreferences sha=getSharedPreferences("name",MODE_PRIVATE);    boolean  isNight=sha.getBoolean("night",false);    if (isNight){        //白天模式        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);        sha.edit().putBoolean("night",false).commit();        bu.setText("切换到夜间模式");    }else{    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);        sha.edit().putBoolean("night",true).commit();        bu.setText("切换到白天模式");    }      recreate();//最后的这个绝对不能忘,不然切换回失效的}


 
原创粉丝点击