2.2 Supporting Different Screens 支持不同的屏幕
Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density. As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.
Android使用两种常规属性对设备屏幕进行分类:大小和密度。 您应该期望您的应用程序将安装在具有尺寸和密度范围的屏幕的设备上。 因此,您应该添加一些替代资源,优化您的应用程序的外观,以适应不同的屏幕尺寸和密度。
- There are four generalized sizes: small, normal, large, xlarge
- 有四种广义尺寸: small, normal, large, xlarge
- And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)
- 和四个广义密度: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)
To declare different layouts and bitmaps you'd like to use for different screens, you must place these alternative resources in separate directories, similar to how you do for different language strings.
要声明您要用于不同屏幕的不同布局和位图,您必须将这些替代资源放在不同的目录中,与您对不同语言字符串的操作类似。
Also be aware that the screens orientation (landscape or portrait) is considered a variation of screen size, so many apps should revise the layout to optimize the user experience in each orientation.
还要注意,屏幕方向(横向或纵向)被认为是屏幕尺寸的变化,所以许多应用程序应该修改布局,以优化每个方向的用户体验。
Create Different Layouts
创建不同的布局
To optimize your user experience on different screen sizes, you should create a unique layout XML file for each screen size you want to support. Each layout should be saved into the appropriate resources directory, named with a -<screen_size>
suffix. For example, a unique layout for large screens should be saved under res/layout-large/
.
要优化不同屏幕尺寸的用户体验,您应该为每个要支持的屏幕尺寸创建一个独特的布局XML文件。 每个布局应该保存到适当的资源目录中,命名为 - -<screen_size>
后缀。 例如,大屏幕的独特布局应该保存在res/layout-large/
下。
Note: Android automatically scales your layout in order to properly fit the screen. Thus, your layouts for different screen sizes don't need to worry about the absolute size of UI elements but instead focus on the layout structure that affects the user experience (such as the size or position of important views relative to sibling views).
注意:Android会自动缩放布局,以适应屏幕。 因此,不同屏幕尺寸的布局不需要担心UI元素的绝对大小,而是重点关注影响用户体验的布局结构(例如相对于兄弟视图的重要视图的大小或位置)。
For example, this project includes a default layout and an alternative layout for large screens:
例如,该项目包括大屏幕的默认布局和替代布局:
MyProject/ res/ layout/ main.xml layout-large/ main.xml
The file names must be exactly the same, but their contents are different in order to provide an optimized UI for the corresponding screen size.
文件名必须完全相同,但内容不同,以便为相应的屏幕尺寸提供优化的UI。
Simply reference the layout file in your app as usual:
只需像往常一样在应用中引用布局文件:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
The system loads the layout file from the appropriate layout directory based on screen size of the device on which your app is running. More information about how Android selects the appropriate resource is available in the Providing Resources guide.
系统根据您的应用程序运行的设备的屏幕尺寸,从相应的布局目录加载布局文件。 有关Android选择适当资源的更多信息,请参阅“Providing Resources”指南。
As another example, here's a project with an alternative layout for landscape orientation:
另一个例子,这是一个具有横向导向的替代布局的项目:
MyProject/ res/ layout/ main.xml layout-land/ main.xml
By default, the layout/main.xml
file is used for portrait orientation.
默认情况下,layout/main.xml
文件用于纵向。
If you want to provide a special layout for landscape, including while on large screens, then you need to use both the large
and land
qualifier:
如果你想为横向提供一个特殊的布局,包括在大屏幕上,那么你需要同时使用large
和land
限定词:
MyProject/ res/ layout/ # default (portrait) main.xml layout-land/ # landscape main.xml layout-large/ # large (portrait) main.xml layout-large-land/ # large landscape main.xml
Note: Android 3.2 and above supports an advanced method of defining screen sizes that allows you to specify resources for screen sizes based on the minimum width and height in terms of density-independent pixels. This lesson does not cover this new technique. For more information, read Designing for Multiple Screens.
注意:Android 3.2及更高版本支持定义屏幕尺寸的高级方法,允许您根据与密度无关的像素的最小宽度和高度来指定屏幕尺寸的资源。 本课不涵盖这一新技术。 有关详细信息,请参阅Designing for Multiple Screens。
Create Different Bitmaps
创建不同的位图
You should always provide bitmap resources that are properly scaled to each of the generalized density buckets: low, medium, high and extra-high density. This helps you achieve good graphical quality and performance on all screen densities.
您应该始终提供适当缩放到每个广义密度的位图资源:低,中,高和超高密度。 这有助于您在所有屏幕密度上实现良好的图形质量和性能。
To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:
要生成这些图像,您应该以矢量格式从原始资源开始,并使用以下尺寸比例为每个密度生成图像:
- xhdpi: 2.0
- hdpi: 1.5
- mdpi: 1.0 (baseline)
- ldpi: 0.75
This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.
这意味着如果您为xhdpi设备生成200x200图像,则hdpi应为150x150,mdpi为100x100,ldpi为75x75。
Then, place the files in the appropriate drawable resource directory:
然后,将文件放在相应的drawable资源目录中:
MyProject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png drawable-ldpi/ awesomeimage.png
Any time you reference @drawable/awesomeimage
, the system selects the appropriate bitmap based on the screen's density.
每当您引用@drawable/awesomeimage
时,系统将根据屏幕的密度选择适当的位图。
Note: Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.
注意:低密度(ldpi)资源并不总是必需的。 当您提供hdpi资产时,系统将其缩小一半以适应ldpi屏幕。
For more tips and guidelines about creating icon assets for your app, see the Iconography design guide.
有关为您的应用创建图标资源的更多提示和指南,请参阅Iconography design guide。