使用夜间切换,切换回来后为什么是灰色的?不是白色的,是哪里的配置文件有问题吗?

colors:

#3F51B5
#303F9F
#FF4081
<!--定义需要切换的两种颜色,使用只要调用对应的name就可以了-->

<color name="textColor">#3b3b3b</color>
<color name="textColor_night">#FFFFFF</color>
<color name="backgroundColor">#FFFFFF</color>
<color name="backgroundColor_night">#3b3b3b</color>

布局
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativelayout"
android:background="#55666666"
tools:context="text.bawei.com.heibai.MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="黑白切换"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

package text.bawei.com.heibai;

import android.content.Context;
import android.content.res.Resources;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**

  • 类描述:
  • 姓名 :刘希鑫 */

public class Heibaiqiehuan {
private static ThemeMode mThemeMode = ThemeMode.DAY;
private static List mThemeChangeListenerList = new LinkedList<>();
private static HashMap> sCachedNightResrouces = new HashMap<>();
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();
}
}
}
}
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
 */
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();
}

}

main类
package text.bawei.com.heibai;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

//实现的接口是自己刚刚写的那个类
public class MainActivity extends AppCompatActivity implements Heibaiqiehuan.OnThemeChangeListener{
Button button;
private boolean isDay=true;//默认是日间模式
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//还是用自己定义的那个类来调用方法
Heibaiqiehuan.registerThemeChangeListener(this);

    button = (Button) findViewById(R.id.button);

    relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //再点击事件里面切换黑白
            if(isDay==true){
                Heibaiqiehuan.setThemeMode(Heibaiqiehuan.ThemeMode.NIGHT );
                button.setText("日间模式");
                relativeLayout.setBackgroundColor(Color.BLACK);
                isDay=false;
            }else {
                Heibaiqiehuan.setThemeMode(Heibaiqiehuan.ThemeMode.DAY );
                relativeLayout.setBackgroundColor(Color.GRAY);
                button.setText("夜间模式");
                isDay=true;
            }
        }
    });




}

@Override
public void onThemeChanged() {
    //日间模式下的颜色
    button.setTextColor(getResources().getColor(Heibaiqiehuan.getCurrentThemeRes(MainActivity.this, R.color.textColor)));
    relativeLayout.setBackgroundColor(getResources().getColor(Heibaiqiehuan.getCurrentThemeRes(MainActivity.this, R.color.backgroundColor)));

}
@Override
protected void onDestroy() {
    super.onDestroy();
    Heibaiqiehuan.unregisterThemeChangeListener(this);
}

}

查看全部
Liu_ser
Liu_ser
2017/07/19 13:18
  • 编码
  • 点赞
  • 收藏
  • 回答
    私信

0个回复