[英]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.