实现夜间模式,不需要recreate,不闪屏

通过资源 id 映射,回调自定义 ThemeChangeListener 接口来处理日间/夜间模式的切换。


在values下面colors.xml里面

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.      
  4.  <color name="colorPrimary">#3F51B5</color>    
  5.  <color name="colorPrimary_night">#3b3b3b</color>    
  6.  <color name="colorPrimaryDark">#303F9F</color>    
  7.  <color name="colorPrimaryDark_night">#383838</color>    
  8.  <color name="colorAccent">#FF4081</color>    
  9.  <color name="colorAccent_night">#a72b55</color>    
  10.  <color name="textColor">#FF000000</color>    
  11.  <color name="textColor_night">#FFFFFF</color>    
  12.  <color name="backgroundColor">#FFFFFF</color>    
  13.  <color name="backgroundColor_night">#3b3b3b</color>    
  14.      
  15. </resources>    
需要一个主题管理类 ThemeManager

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

在MainActivity.java找到按钮的id和根布局的id

在 MainActivity 中实现了 OnThemeChangeListener 接口,这样就可以在主题改变的时候执行回调方法。然后在 initTheme() 中去重新设置 UI 的相关颜色属性值。还有别忘了要在 onDestroy() 中移除 ThemeChangeListener 。

[html] view plain copy
  1. public class MainActivity extends AppCompatActivity implements ThemeManager.OnThemeChangeListener {    
  2.     
  3.  private TextView tv;    
  4.  private Button btn_theme;    
  5.  private RelativeLayout relativeLayout;    
  6.  private ActionBar supportActionBar;    
  7.     
  8.  @Override    
  9.  protected void onCreate(Bundle savedInstanceState) {    
  10.   super.onCreate(savedInstanceState);    
  11.   setContentView(R.layout.activity_main);    
  12.   ThemeManager.registerThemeChangeListener(this);    
  13.   supportActionBar = getSupportActionBar();    
  14.   btn_theme = (Button) findViewById(R.id.btn_theme);    
  15.   relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);    
  16.   tv = (TextView) findViewById(R.id.tv);    
  17.   btn_theme.setOnClickListener(new View.OnClickListener() {    
  18.    @Override    
  19.    public void onClick(View v) {    
  20.     ThemeManager.setThemeMode(ThemeManager.getThemeMode() == ThemeManager.ThemeMode.DAY    
  21.       ? ThemeManager.ThemeMode.NIGHT : ThemeManager.ThemeMode.DAY);    
  22.    }    
  23.   });    
  24.  }    
  25.     
  26.  public void initTheme() {    
  27.   tv.setTextColor(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.textColor)));    
  28.   btn_theme.setTextColor(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.textColor)));    
  29.   relativeLayout.setBackgroundColor(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.backgroundColor)));    
  30.   // 设置标题栏颜色    
  31.   if(supportActionBar != null){    
  32.    supportActionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.colorPrimary))));    
  33.   }    
  34.   // 设置状态栏颜色    
  35.   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {    
  36.    Window window = getWindow();    
  37.    window.setStatusBarColor(getResources().getColor(ThemeManager.getCurrentThemeRes(MainActivity.this, R.color.colorPrimary)));    
  38.   }    
  39.  }    
  40.     
  41.  @Override    
  42.  public void onThemeChanged() {    
  43.   initTheme();    
  44.  }    
  45.     
  46.  @Override    
  47.  protected void onDestroy() {    
  48.   super.onDestroy();    
  49.   ThemeManager.unregisterThemeChangeListener(this);    
  50.  }    
  51.     
  52. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值