如何使用代码确定设备屏幕尺寸类别(小、正常、大、xlarge)?
有没有办法判断当前设备屏幕尺寸的类别,比如小,正常,大,xlarge?
不是密度,而是屏幕尺寸。
Is there any way to determine the category of screen size of the current device, such as small, normal, large, xlarge?
Not the density, but the screen size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用
Configuration.screenLayout
< /a> 位掩码。例子:
You can use the
Configuration.screenLayout
bitmask.Example:
下面的代码充实了上面的答案,将屏幕尺寸显示为Toast。
下面的代码将屏幕密度显示为Toast。
The code below fleshes out the answer above, displaying the screen size as a Toast.
This code below displays the screen density as a Toast.
Jeff Gilfelt 的答案作为静态辅助方法:
Jeff Gilfelt's answer as a static helper method:
感谢上面的答案,这对我帮助很大:-)但是对于那些被迫仍然支持 Android 1.5 的人(像我一样),我们可以使用 java 反射来向后兼容:
Thanks for the answers above, that helped me a lot :-) But for those (like me) forced to still support Android 1.5 we can use java reflection for backward compatible:
2018 年,如果您需要 Jeff 在 Kotlin 中的回答,这里是:
In 2018, if you need Jeff's answer in Kotlin, here it is:
需要检查是否有超大屏幕和x..高密度?这是所选答案的更改代码。
Need to check for xlarge screens & x..high densities? This is the altered code from the chosen answer.
这是 Tom McFarlin 的回答的 Xamarin.Android 版本
Here is a Xamarin.Android version of Tom McFarlin's answer
试试这个函数 isLayoutSizeAtLeast(int screenSize)
要检查小屏幕,至少 320x426 dp 及以上
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_SMALL);
要检查正常屏幕,至少 320x470 dp 及以上
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL);
要检查大屏幕,至少 480x640 dp 及以上
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
要检查超大屏幕,至少 720x960 dp 及以上
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE);
Try this function isLayoutSizeAtLeast(int screenSize)
To check small screen, atleast 320x426 dp and above
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_SMALL);
To check normal screen, atleast 320x470 dp and above
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL);
To check large screen, atleast 480x640 dp and above
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
To check extra large screen, atleast 720x960 dp and above
getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE);
你不能使用字符串资源和枚举来做到这一点吗?您可以定义具有屏幕尺寸名称的字符串资源,例如 SMALL、MEDIUM 或 LARGE。然后,您可以使用字符串资源的值来创建枚举的实例。
在代码中为您关心的不同屏幕尺寸定义一个枚举。
定义一个字符串资源,例如屏幕大小,其值可以是 SMALL、MEDIUM 或 LARGE。
screensize
的副本放入您关心的每个维度的字符串资源中。例如,
MEDIUM
将放入values-sw600dp/strings.xml 和values-medium/strings.xml 和LARGE
将放入 sw720dp/strings.xml 和 value-large/strings.xml 中。ScreenSize 大小 = ScreenSize.valueOf(getReources().getString(R.string.screensize);
Couldn't you do this using a string resource and enums? You can define a string resource that had the name of the screen size, such as SMALL, MEDIUM, or LARGE. Then you could use the value of the string resource to create an instance of the enum.
Define an Enum in your code for the different screen sizes you care about.
Define a string resource, say screensize, whose value will be either SMALL, MEDIUM, or LARGE.
screensize
in a string resource in each dimension you care about.For example,
<string name="screensize">MEDIUM</string>
would go in values-sw600dp/strings.xml and values-medium/strings.xml and<string name="screensize">LARGE</string>
would go in sw720dp/strings.xml and values-large/strings.xml.ScreenSize size = ScreenSize.valueOf(getReources().getString(R.string.screensize);