简体   繁体   English

检测 Android 设备主题(是深色还是浅色)

[英]Detect the Android device theme (is it dark or light)

You can set the theme for your app, but I'm wondering if it's possible to find out which one is used by the device.您可以为您的应用设置主题,但我想知道是否可以找出设备使用的是哪个主题。 Currently, my app uses Theme.AppCompat.Light .目前,我的应用程序使用Theme.AppCompat.Light I'd like to avoid changing the theme.我想避免改变主题。

PS I've already tried to set it to Theme.DeviceDefault and access its ID using reflection, but no luck so far. PS 我已经尝试将其设置为Theme.DeviceDefault并使用反射访问其 ID,但到目前为止还没有运气。

try {
    setTheme(android.R.style.Theme_DeviceDefault);

    Class<Context> contextClass = Context.class;
    Method getThemeMethod = contextClass.getMethod("getThemeResId");
    getThemeMethod.setAccessible(true);
    Log.d("Test", "" + getThemeMethod.invoke(this)); // Always returns 0

    PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
    Log.d("Test", getResources().getResourceEntryName(packageInfo.applicationInfo.theme)); // Returns 'Theme.DeviceDefault'
} catch (Exception e) {
    Log.d("Test", "exception", e);
}
int currentNightMode = getContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;  
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active on device
        break;
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active on device
        break;
}    

这可能有助于ContextThemeWrapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 检测用户的操作系统主题是深色还是浅色? - Android - Detect if the user's OS theme is dark or light? 如何检测手机系统是深色主题还是浅色主题? - How to detect phone system is dark theme or light theme? Android 浅色/深色主题操作栏文字 - Android light/dark theme action bar text 支持Android Pie中的明暗主题 - Support both light and dark theme in Android Pie Android Theme.AppCompat.Light with Dark Toolbar(用于浅色文本) - Android Theme.AppCompat.Light with Dark Toolbar (for light text) 是否有 API 来检测操作系统正在使用哪个主题 - 暗或亮(或其他)? - Is there an API to detect which theme the OS is using - dark or light (or other)? 深色主题和浅色主题问题——android studio(夜间) - Dark theme and light theme problem - android studio (night) 如何在深色背景上使用材质灯光主题设置Android Spinner的样式? - How to style Android Spinner with Material light theme on a dark background? Android。 在浅色主题中使用深色主题 colors - Android. Using dark themed colors in Light Theme 将android工具栏弹出菜单主题从黑暗变为光 - change android toolbar popup menu theme from dark to light
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM