android – 使用不忽略night限定符的ContextCompat以编程方式从资源中获取颜色

我目前正在使用ContextCompat.getColor,但它没有获取正确的颜色.当应用程序遵守night资源限定符时,ContextCompat从values / colors.xml而不是values-night / colors.xml中获取颜色.

我尝试了像这个https://stackoverflow.com/a/13952929/333733这样的apporaches使用一个主题,其中一个位于values / styles.xml和values-night / styles.xml中,但似乎使用没有-night限定符的资源文件夹预编译颜色.

最佳答案 您可以检查主题在您自己的模式:

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
}

资料来源:https://medium.com/@chrisbanes/appcompat-v23-2-daynight-d10f90c83e94#.l2fswuy4z

点赞