我认为您可以使用 UiModeManager 类来获取当前模式。尽管无法确定用户在“自动”模式下是否处于黑暗模式。让我们看一下代码 -
UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
int modeType = uiModeManager.getNightMode();
您还可以从下面的代码中进行模式输入 -
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
}
之后,您可以根据模式更改文本颜色。但我找到了另一种替代方法来完成你的工作。就像根据模式改变你的资源风格一样,就像在你的资源/值/themes.xml file-
<style name="Theme.AppCompat.DayLight"
parent="Theme.AppCompat.Light" />
还有另一个文件res/values-night/themes.xml -
<style name="Theme.AppCompat.Night"
parent="Theme.AppCompat.DayNight" />
您可以尝试不同的属性,例如唯一的Theme.AppCompat
代替Theme.AppCompat.DayNight
。实际上我没有运行这段代码,但似乎只需进行一些更改即可工作。
请在安卓上查看一下文档 https://developer.android.com/reference/android/app/UiModeManager, this https://stackoverflow.com/questions/41391404/how-to-get-appcompatdelegate-current-mode-if-default-is-autostackoverflow问题和这个媒体blog https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94.