Android 获取设备宽度:解决 Context.getDisplay().getMode().getPhysicalWidth() 低于 30 的问题
如果 'Context.getDisplay().getMode().getPhysicalWidth()' 低于 30,则可能表示您的设备宽度非常小或者出现了错误。您可以尝试使用其他方法来获取设备宽度,例如:
- 使用 DisplayMetrics 类获取设备宽度
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
- 使用 View 类获取设备宽度
View view = getWindow().getDecorView().findViewById(android.R.id.content);
int width = view.getWidth();
请注意,在使用这些方法之前,您需要确保您的 Activity 已经被创建并且正在运行。如果问题仍然存在,请检查您的设备是否正常工作。
原文地址: https://www.cveoy.top/t/topic/nDFJ 著作权归作者所有。请勿转载和采集!