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使用两个通用的性质来为设备屏幕进行分类:大小和密度。你应该期望你的app可以在一定范围内的大小和密度的设备上都可以安装。同样的,你应该包含一些可供选择的资源来为不同的屏幕大小和密度来完善你的app外观。


  • There are four generalized sizes: small, normal, large, xlarge 有四种通用的大小:小,正常,大,很大
  • And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)以及四种通用的密度:低(ldpi),中等(mdpi),高(hdpi),很高(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.

你同样要意识到,屏幕的方向(横向或纵向)也是一种屏幕大小,因此很多app应该修正布局来完善每个方向上的用户体验。


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元素的绝对大小,而应该关注布局结构,这会影响到用户体验(例如重要的views相对于其他views的大小或位置)。

For example, this project includes a default layout and an alternative layout forlarge screens:

例如,这个项目包含了一个默认布局和一个供选择的为large屏幕提供的布局:

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:

你可以像往常一样简单地在你的app中引用布局文件:

@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.

系统根据你的app运行在什么样的屏幕大小来决定从哪个合适的布局目录下加载布局文件。更多有关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.

你应该总是为每个通用的密度类型提供可以合适缩放的位图:low,medium,high和extra-high 密度。这可以帮助你在所有的屏幕密度上有很好的图片质量和性能。

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、mdpi、ldpi分别生成150X150、100X100、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 ​​.

更多的关于为你的app创建图标资源的技巧和指南,参见​Iconography design guide​