现在好多app都有日夜切换效果,下面就是日夜切换的一个简单实现哈,
点击一个button按钮进行日夜切换
1.新建一个工具类ThemManager
package com.example.daynightthemdemo;
import android.content.Context;
import android.content.res.Resources;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class ThemManager {
private static ThemeMode mThemeMode = ThemeMode.DAY;
// 主题模式监听器
private static List mThemeChangeListenerList = new LinkedList<>();
// 夜间资源的缓存,key : 资源类型, 值
private static HashMap> sCachedNightResrouces = new HashMap<>();
// 夜间模式资源的后缀,比如日件模式资源名为:R.color.activity_bg, 那么夜间模式就为 :R.color.activity_bg_night
private static final String RESOURCE_SUFFIX = "_night";
/**
* 主题模式,分为日间模式和夜间模式
*/
public enum ThemeMode {
DAY, NIGHT
}
/**
* 设置主题模式
*
* @param themeMode
*/
public static void setThemeMode(ThemeMode themeMode) {
if (mThemeMode != themeMode) {
mThemeMode = themeMode;
if (mThemeChangeListenerList.size() > 0) {
for (OnThemeChangeListener listener : mThemeChangeListenerList) {
listener.onThemeChanged();
}
}
}
}
/**
* 根据传入的日间模式的resId得到相应主题的resId,注意:必须是日间模式的resId
*
* @param dayResId 日间模式的resId
* @return 相应主题的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 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
*
* @param listener
*/
public static void registerThemeChangeListener(OnThemeChangeListener listener) {
if (!mThemeChangeListenerList.contains(listener)) {
mThemeChangeListenerList.add(listener);
}
}
/**
* 反注册ThemeChangeListener
*
* @param listener
*/
public static void unregisterThemeChangeListener(OnThemeChangeListener listener) {
if (mThemeChangeListenerList.contains(listener)) {
mThemeChangeListenerList.remove(listener);
}
}
/**
* 得到主题模式
*
* @return
*/
public static ThemeMode getThemeMode() {
return mThemeMode;
}
/**
* 主题模式切换监听器
*/
public interface OnThemeChangeListener {
/**
* 主题切换时回调
*/
void onThemeChanged();
}
}
2.colors.xml
#3F51B5
#3b3b3b
#303F9F
#383838
#FF4081
#a72b55
#FF000000
#FFFFFF
#FFFFFF
#3b3b3b
3.MainActivity中
package com.example.daynightthemdemo;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ThemManager.OnThemeChangeListener {
private ActionBar supportActionBar;
private Button change;
private RelativeLayout rela;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// getApplication().getResources().getIdentifier(String name, String defType, String defPackage)
ThemManager.registerThemeChangeListener(this);//一定的调用
supportActionBar = MainActivity.this.getSupportActionBar();
}
private void initView() {
change = (Button) findViewById(R.id.change);
change.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.change:
ThemManager.setThemeMode(ThemManager.getThemeMode() == ThemManager.ThemeMode.DAY
? ThemManager.ThemeMode.NIGHT : ThemManager.ThemeMode.DAY);
break;
}
}
@Override
public void onThemeChanged() {
// textView.setTextColor(getResources().getColor(ThemManager.getCurrentThemeRes(getContext(), R.color.textColor)));
//btn_theme.setTextColor(getResources().getColor(ThemeManager.getCurrentThemeRes(getContext(), R.color.textColor)));
// rel.setBackgroundColor(getResources().getColor(ThemManager.getCurrentThemeRes(getContext(), R.color.backgroundColor)));
// ((Main2Activity) getActivity()).findViewById(R.id.tab_layout).setBackgroundColor(getResources().getColor(ThemManager.getCurrentThemeRes(getContext(), R.color.backgroundColor)));
// ((Main2Activity) getActivity()).findViewById(R.id.tab_layout).setBackgroundColor(getResources().getColor(ThemManager.getCurrentThemeRes(getContext(), R.color.backgroundColor)));
// ((Main2Activity) getActivity()).findViewById(R.id.tab_layout).setBackgroundColor(getResources().getColor(ThemManager.getCurrentThemeRes(getContext(), R.color.backgroundColor)));
// ((Main2Activity) getActivity()).findViewById(R.id.tab_layout).setBackgroundColor(getResources().getColor(ThemManager.getCurrentThemeRes(getContext(), R.color.backgroundColor)));
MainActivity.this.findViewById(R.id.rela).setBackgroundColor(getResources().getColor(ThemManager.getCurrentThemeRes(MainActivity.this, R.color.backgroundColor)));
// 设置标题栏颜色
if (supportActionBar != null) {
supportActionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(ThemManager.getCurrentThemeRes(MainActivity.this, R.color.colorPrimary))));
}
// 设置状态栏颜色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = MainActivity.this.getWindow();
window.setStatusBarColor(getResources().getColor(ThemManager.getCurrentThemeRes(MainActivity.this, R.color.colorPrimary)));
}
}
//移除 ThemeChangeListener
@Override
protected void onDestroy() {
super.onDestroy();
ThemManager.unregisterThemeChangeListener(this);
}
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
以上代码有copy的,如有不妥,联系欧啦 哈!!!!!!