我有这样的活动:
package com.nkdroid.daynighttheme;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.TextView;
public class ModeActivity extends AppCompatActivity {
private TextView txtModeType;
int modeType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_mode);
txtModeType = (TextView) findViewById(R.id.txtModeType);
modeType = AppCompatDelegate.getDefaultNightMode();
if (modeType == AppCompatDelegate.MODE_NIGHT_AUTO) {
txtModeType.setText("Default Mode: Auto");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_YES) {
txtModeType.setText("Default Mode: Night");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_NO) {
txtModeType.setText("Default Mode: Day");
}
}
}`
如果默认模式设置为AUTO,现在是否可以获得哪个模式(白天还是夜晚)处于活动状态?
发布于 2017-01-03 19:42:04
您可以使用以下代码获取当前模式,
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're at night!
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// We don't know what mode we're in, assume notnight
}
下面由Chris编写的article很好地解释了这一点。
发布于 2019-10-23 14:31:21
不知何故,@harshithdwivedi的回答在应用程序内部设置夜间模式(使用AppCompatDelegate
)时对我无效。否则效果很好。
因此,我不得不添加一些额外的检查如下:
public static boolean isNightModeActive(Context context) {
int defaultNightMode = AppCompatDelegate.getDefaultNightMode();
if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_YES) {
return true;
}
if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_NO) {
return false;
}
int currentNightMode = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return false;
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
return false;
}
return false;
}
发布于 2019-06-25 08:40:46
如果您是kotlin开发人员,那么您可以使用下面的代码来检查应用程序所处的模式。
val mode = context?.resources?.configuration?.uiMode? and Configuration.UI_MODE_NIGHT_MASK
when (mode) {
Configuration.UI_MODE_NIGHT_YES -> {}
Configuration.UI_MODE_NIGHT_NO -> {}
else -> {} //covers Configuration.UI_MODE_NIGHT_UNDEFINED
}
有关黑暗主题模式的更多信息,请参见;
https://stackoverflow.com/questions/41391404
复制相似问题