首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果默认情况是自动的,如何获得AppCompatDelegate当前模式

如果默认情况是自动的,如何获得AppCompatDelegate当前模式
EN

Stack Overflow用户
提问于 2016-12-30 06:03:06
回答 5查看 8.9K关注 0票数 26

我有这样的活动:

代码语言:javascript
运行
复制
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,现在是否可以获得哪个模式(白天还是夜晚)处于活动状态?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2017-01-03 19:42:04

您可以使用以下代码获取当前模式,

代码语言:javascript
运行
复制
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很好地解释了这一点。

票数 41
EN

Stack Overflow用户

发布于 2019-10-23 14:31:21

不知何故,@harshithdwivedi的回答在应用程序内部设置夜间模式(使用AppCompatDelegate)时对我无效。否则效果很好。

因此,我不得不添加一些额外的检查如下:

代码语言:javascript
运行
复制
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;
}
票数 16
EN

Stack Overflow用户

发布于 2019-06-25 08:40:46

如果您是kotlin开发人员,那么您可以使用下面的代码来检查应用程序所处的模式。

代码语言:javascript
运行
复制
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://github.com/googlesamples/android-DarkTheme/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41391404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档