android开发暗黑模式开发,android中的强制黑暗模式没有响应

I am trying to implement dark theme as described here. So, I have created arrays.xml as:

"Default"

"Light"

"Dark"

"Default"

"Light"

"Dark"

and this is my code to change the theme, which is not working, i.e., the theme is not changing, though the first Toast is giving correct value, where the 2nd Toast before switch gives some mysterious value like 16/32 etc.

I understand that, my value array is key, where I amy trying to read some int in the switch. But I am not getting how to get that.

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

String theme = sharedPref.getString("theme", "Default");

Toast.makeText(this, theme, Toast.LENGTH_LONG).show();

int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;

Toast.makeText(this, ""+currentNightMode, Toast.LENGTH_LONG).show();

switch (currentNightMode) {

case Configuration.UI_MODE_NIGHT_NO:

// Night mode is not active, we're using the light theme

break;

case Configuration.UI_MODE_NIGHT_YES:

// Night mode is active, we're using dark theme

break;

default:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

}

解决方案

Try below dark mode code which I am use.

Step - 1

First of create night folder into your resource file like below image(i.e. values-night)

0WfcS.png

Step - 2

Create styles,strings and colors xml file for night mode same as below image and add your night mode color,string and style which you want to show in your app when night mode was apply.

elmOM.png

Step - 3

Add this below code in your splash screen if you want to set night mode as per device mode first time when application installed.

@Override

protected void onCreate(Bundle savedInstanceState) {

if (!CommonUtils.isToogleEnabled(SplashActivity.this)) {

if (CommonUtils.isDarkMode(SplashActivity.this)) {

CommonUtils.setIsNightModeEnabled(SplashActivity.this, true);

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

} else {

CommonUtils.setIsNightModeEnabled(SplashActivity.this, false);

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

}

} else {

if (CommonUtils.isNightModeEnabled(SplashActivity.this)) {

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

} else {

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

}

}

super.onCreate(savedInstanceState);

...

//your code

}

Step - 4

Add this below code in your all activity

@Override

protected void onCreate(Bundle savedInstanceState) {

if (CommonUtils.isNightModeEnabled(MainActivity.this)) {

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

} else {

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

}

super.onCreate(savedInstanceState);

...

//your code

}

Step - 5

Change mode using below code

private WeakReference mActivity;

binding.imgNightMode.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

mActivity = new WeakReference(MainActivity.this);

CommonUtils.setIsToogleEnabled(MainActivity.this, true);

if (CommonUtils.isNightModeEnabled(MainActivity.this)) {

CommonUtils.setIsNightModeEnabled(MainActivity.this, false);

mActivity.get().recreate();

} else {

CommonUtils.setIsNightModeEnabled(MainActivity.this, true);

mActivity.get().recreate();

}

}

});

Below methods are CommonUtils.

private static final String NIGHT_MODE = "NIGHT_MODE";

private static final String TOOGLE = "TOOGLE";

public static boolean isNightModeEnabled(Context context) {

SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);

return mPrefs.getBoolean(NIGHT_MODE, false);

}

public static void setIsNightModeEnabled(Context context, boolean isNightModeEnabled) {

SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);

SharedPreferences.Editor editor = mPrefs.edit();

editor.putBoolean(NIGHT_MODE, isNightModeEnabled);

editor.apply();

}

public static void setIsToogleEnabled(Context context, boolean isToogleEnabled) {

SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);

SharedPreferences.Editor editor = mPrefs.edit();

editor.putBoolean(TOOGLE, isToogleEnabled);

editor.apply();

}

public static boolean isToogleEnabled(Context context) {

SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);

return mPrefs.getBoolean(TOOGLE, false);

}

public static boolean isDarkMode(Activity activity) {

return (activity.getResources().getConfiguration()

.uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;

}

I hope this can help you!

Thank You.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值