简体   繁体   English

如何知道要使用哪个布局限定符?

[英]How to know which layout qualifier to use?

为特定的Android设备找到最佳布局限定符的最佳方法是什么?

There are no resource qualifiers that are device-specific. 没有特定于设备的资源限定符。 For example, you can't specify layout-neuxs-5 . 例如,您不能指定layout-neuxs-5

This is because you rarely want to change a layout for one specific device when there are likely dozens of other similar devices that need a similar layout (or other set of resources). 这是因为当可能有许多其他类似设备需要类似布局(或其他资源集)时,您很少希望更改一个特定设备的布局。

The first step then is identifying what exactly defines the type of device that needs a different resource. 然后,第一步是确定什么定义了需要不同资源的设备类型。 You can provide different resources of any type (layout, drawable, string, etc.) for any sort of qualifier you can imagine- language, screen orientation, screen size, screen density, and more. 您可以为您可以想象的任何类型的限定符提供任何类型(布局,可绘制,字符串等)的不同资源,包括语言,屏幕方向,屏幕大小,屏幕密度等等。

While you didn't provide any specifics, I'm going to assume that you are looking to provide different layouts for differently sized screens. 尽管您未提供任何细节,但我将假设您正在为不同尺寸的屏幕提供不同的布局。 For this, you generally want to use the smallest-width qualifier (eg layout-sw320dp , layout-sw720dp ). 为此,通常需要使用最小宽度的限定符(例如layout-sw320dplayout-sw720dp )。

If that's the case, then the real question you are asking is likely more along the lines of "how do I know what size screen a device has?" 如果是这样,那么您要问的真正问题可能更多是“我如何知道设备的屏幕尺寸?”。 To calculate that, you need to know two things: 要进行计算,您需要了解两件事:

  • The device's density class (mdpi, xhdpi, etc., which you can calculate based on the physical screen size and resolution) 设备的密度等级(mdpi,xhdpi等,您可以根据物理屏幕尺寸和分辨率进行计算)
  • The device's resolution 设备的分辨率

Let's look at the Nexus 5. It is and xxhdpi device and its resolution is 1080 x 1920 pixels. 让我们看一下Nexus5。它是xxhdpi设备,分辨率为1080 x 1920像素。 xxhdpi is a 3x scale, and the smallest dimension is 1080, thus 1080 divided by 3 = 360dp. xxhdpi是3x比例,最小尺寸是1080,因此1080除以3 = 360dp。 The smallest width of a Nexus 5 is 360dp. Nexus 5的最小宽度为360dp。

Thus to provide resources that are at least the approximate size of a Nexus 5, you would put those resources in a folder with the -sw360dp qualifier. 因此,要提供至少与Nexus 5近似大小的资源,可以将这些资源放入带有-sw360dp限定符的文件夹中。

Here are some common sizes suggested by the documentation: 以下是文档建议的一些常见尺寸:

  • 320, for devices with screen configurations such as: 320,用于具有以下屏幕配置的设备:
    • 240x320 ldpi (QVGA handset) 240x320 ldpi(QVGA手机)
    • 320x480 mdpi (handset) 320x480 mdpi(手机)
    • 480x800 hdpi (high-density handset) 480x800 hdpi(高密度手机)
  • 480, for screens such as 480x800 mdpi (tablet/handset). 480,适用于480x800 mdpi(平板电脑/手机)之类的屏幕。
  • 600, for screens such as 600x1024 mdpi (7" tablet). 600,用于600x1024 mdpi(7英寸平板电脑)之类的屏幕。
  • 720, for screens such as 720x1280 mdpi (10" tablet). 720,适用于720x1280 mdpi(10英寸平板电脑)之类的屏幕。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM