Supporting Multiple Screens 支持各种各样的屏幕尺寸、屏幕密度
Android runs on a variety of devices that offer different screen sizes and densities. For applications, the Android system provides a consistent development environment across devices and handles most of the work to adjust each application‘s user interface to the screen on which it is displayed. At the same time, the system provides APIs that allow you to control your application‘s UI for specific screen sizes and densities, in order to optimize your UI design for different screen configurations. For example, you might want a UI for tablets that‘s different from the UI for handsets.
Although the system performs scaling and resizing to make your application work on different screens, you should make the effort to optimize your application for different screen sizes and densities. In doing so, you maximize the user experience for all devices and your users believe that your application was actually designed for theirdevices—rather than simply stretched to fit the screen on their devices.
By following the practices described in this document, you can create an application that displays properly and provides an optimized user experience on all supported screen configurations, using a single .apk
file.
android运行在不同的屏幕尺寸和屏幕密度(译者注:屏幕密度=(屏幕宽像素的平方+屏幕长像素的平方)开根号/屏幕对角线物理长度,其实屏幕密度可以理解为不同的屏幕材质)。对于应用程序来说,android系统提供相同的开发环境无关具体的手机并且自身处理大多数的不同的要显示的应用程序用户界面。同时,android系统提供APIs允许你去控制你的应用界面为了特殊屏幕尺寸和密度。
为了不同的屏幕配置有最优的UI设计。例如,你可以为了平板电脑和手机做不同的UI设计。
虽然系统会缩放应用来运行在不同的屏幕支持。但你应该为不同屏幕尺寸或不同的密度做不同设计。这样做你可以使不同设备的用户用户体验效果最大化,并且使他们相信你的应用就是为他们的手机而设计,这样会比通过拉伸屏幕来适应他们的手机尺寸效果要好得多。
通过下面这些实践描述,你可以为不同的手机创建显示得当提供最佳用户体验的应用程序。
Note: The information in this document assumes that your application is designed for Android 1.6 (API Level 4) or higher. If your application supports Android 1.5 or lower, please first readStrategies for Android 1.5.
Also, be aware that Android 3.2 has introduced new APIs that allow you to more precisely control the layout resources your application uses for different screen sizes. These new features are especially important if you‘re developing an application that‘s optimized for tablets. For details, see the section about Declaring Tablet Layouts for Android 3.2.
注意:这个文档是为android1.6或更高设计而写,如果你的应用支持1.5或更低,请先看Strategies for Android 1.5.
同时注意android3.2引进的新的APIs允许你更准确的控制不同屏幕尺寸的布局。如果你正开发一个平板应用,这些新的特性是特别重要的。
Overview of Screens Support
屏幕支持总览
This section provides an overview of Android‘s support for multiple screens, including: an introduction to the terms and concepts used in this document and in the API, a summary of the screen configurations that the system supports, and an overview of the API and underlying screen-compatibility features.
这章节提供了多屏幕支持的总览,包括在文档和API中提到的术语和概念,以及系统支持的屏幕配置汇总,还包括API总览和根本屏幕兼容性。
Terms and concepts
术语和概念
Screen size屏幕尺寸Actual physical size, measured as the screen‘s diagonal.
For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra large.
实际的物理尺寸,也就是屏幕的对角线尺寸。
为简单起见,Android把所有的实际此存分成四个普遍的组:小,正常,大,巨大。
Screen density屏幕密度The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen.
For simplicity, Android groups all actual screen densities into four generalized densities: low, medium, high, and extra high.
屏幕一个物理区域上像素点的总数,通常被称作dpi(每英寸的像素点数)。例如,一个低密度屏幕比正常密度和高密度屏幕在给定的物理屏幕区域上的像素点更少。
为简单起见,Android吧所有的屏幕密度分成四个普遍的组:低,中,高,超高。
Orientation方向The orientation of the screen from the user‘s point of view. This is either landscape or portrait, meaning that the screen‘s aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.屏幕方向是从用户的视角来说,横屏屏幕宽高比是宽的,竖屏意味着屏幕宽高比分别是高的,要知道不仅不同设备的默认的操作方向不同,而且在运行中当用户旋转设备时方向可以改变。Resolution分辨率The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.一个屏幕的总的物理像素点。当为多屏幕提供支持时,应用程序不直接用像素工作;我们只应该关心应用程序运行设备的屏幕尺寸和密度,然后指定是哪个组的尺寸和哪个组的密度。Density-independent pixel (dp)逻辑密度单位A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160)
. For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application‘s UI, to ensure proper display of your UI on screens with different densities.
当你设计UI界面的时候你应该用虚拟的像素单位来表示布局的尺寸和位置。当屏幕密度是160dpi是这个逻辑密度单位等于一个物理像素,同时160dpi作为基线密度被系统认为是中等密度屏幕,当必要时系统在运行时显示的处理基于当前屏幕的实际密度的dp单位的任何缩放,dp单位和屏幕像素转换公式是px=dp*(dpi/160)。例如在一个240dpi的屏幕,1dp等于1.5像素,当你设计你应用程序ui时你应该总是用dp单位来处理不同的屏幕密度。
Range of screens supported
屏幕支持的范围
Starting with Android 1.6 (API Level 4), Android provides support for multiple screen sizes and densities, reflecting the many different screen configurations that a device may have. You can use features of the Android system to optimize your application‘s user interface for each screen configuration and ensure that your application not only renders properly, but provides the best user experience possible on each screen.
只从Android1.6开始,Android支持多屏幕尺寸和密度,因为硬件设备可以有许多不同的屏幕尺寸。你能用Android系统的特性为每个不同的屏幕配置来优化你应用程序的用户界面并且确保你的应用程序不仅可以恰当的显示,而且还提供更好的用户体验。
To simplify the way that you design your user interfaces for multiple screens, Android divides the range of actual screen sizes and densities into:
- A set of four generalized sizes: small, normal, large, and xlarge
Note: Beginning with Android 3.2 (API level 13), these size groups are deprecated in favor of a new technique for managing screen sizes based on the available screen width. If you‘re developing for Android 3.2 and greater, see Declaring Tablet Layouts for Android 3.2 for more information.
- A set of four generalized densities: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)
简单的方式:你为多屏幕设计界面,android将实际的屏幕尺寸和密度范围为:
4个广义的尺寸:小,中,大,巨大
注意:从Android3.2开始,这些广义的尺寸分组被新的基于屏幕可以用宽的屏幕管理技术所取代,如果你正开发Android3.2或者更高版本,看Declaring Tablet Layouts for Android 3.2 。
4个广义的密度:低,中,高,超高
The generalized sizes and densities are arranged around a baseline configuration that is a normal size and mdpi(medium) density. This baseline is based upon the screen configuration for the first Android-powered device, the T-Mobile G1, which has an HVGA screen (until Android 1.6, this was the only screen configuration that Android supported).
Each generalized size and density spans a range of actual screen sizes and densities. For example, two devices that both report a screen size of normal might have actual screen sizes and aspect ratios that are slightly different when measured by hand. Similarly, two devices that report a screen density of hdpi might have real pixel densities that are slightly different. Android makes these differences abstract to applications, so you can provide UI designed for the generalized sizes and densities and let the system handle any final adjustments as necessary. Figure 1 illustrates how different sizes and densities are roughly categorized into the different size and density groups.
广义的尺寸和密度分布在基础配置周围,基础配置是指中型屏幕和中型密度。这个基础是以第一个支持Android的硬件设备的T-Mobile G1手机为标准,G1手机有HVGA屏幕(android1.6之前,只支持这种屏幕配置)。
每个广义的尺寸和密度是一个实际屏幕尺寸和密度的范围,例如两个同样是中级尺寸的手机可能在手动测量是屏幕尺寸和屏幕宽高比会有细微的差别。同样两个同样是高级密度的的手机真正的像素密度可能也会有细微差别。Android让这些差别对于应用程序来说无关,所以你可以为广义的屏幕尺寸和密度提供UI设计,如果必要让系统来处理任何最后的调整。图1粗略的表明如何按照不同尺寸密度分类成不同的广义的尺寸和广义的密度。
Figure 1. Illustration of how Android roughly maps actual sizes and densities to generalized sizes and densities (figures are not exact).
As you design your UI for different screen sizes, you‘ll discover that each design requires a minimum amount of space. So, each generalized screen size above has an associated minimum resolution that‘s defined by the system. These minimum sizes are in "dp" units—the same units you should use when defining your layouts—which allows the system to avoid worrying about changes in screen density.
图1粗略的表明如何按照不同尺寸密度分类成不同的广义的尺寸和广义的密度(图标不准确)。
当你为不同屏幕尺寸设计你UI时,你会发现每个设计需要一个最低限度的空间。所以,系统定义每个广义的屏幕尺寸都有最小分辨率。这些最小尺寸用dp单位-当你定义你的布局时你应该用相同的单位,这样你的布局不必担心屏幕密度的变化。
- xlarge screens are at least 960dp x 720dp
- large screens are at least 640dp x 480dp
- normal screens are at least 470dp x 320dp
- small screens are at least 426dp x 320dp
Note: These minimum screen sizes were not as well defined prior to Android 3.0, so you may encounter some devices that are mis-classified between normal and large. These are also based on the physical resolution of the screen, so may vary across devices—for example a 1024x720 tablet with a system bar actually has a bit less space available to the application due to it being used by the system bar.
注意:这些最小屏幕尺寸在android3.0之前没有很好的定义(译者注:此处翻译的未必准确),所以你可能遇到一些设备被归类为既是中屏幕又是大屏幕。这些虽然是基于屏幕的物理分辨率,但是在不同的设备上会有许多变化,例如 1024*720的平板会带一个系统栏,事实上对于应用程序来说可用空间会少一些因为这部分空间应被系统栏占用。
To optimize your application‘s UI for the different screen sizes and densities, you can provide alternative resources for any of the generalized sizes and densities. Typically, you should provide alternative layouts for some of the different screen sizes and alternative bitmap images for different screen densities. At runtime, the system uses the appropriate resources for your application, based on the generalized size or density of the current device screen.
为了为不同的屏幕尺寸和密度优化你的应用程序界面,你可以提供多个资源为任何广义的尺寸和密度。特别是你应该为一些不同的屏幕尺寸提供多套布局并且为不同的屏幕密度提供多种 bitmap图片。在运行时,系统基于当前设备的广义尺寸和密度为你的应用程序提供适当的资源。
You do not need to provide alternative resources for every combination of screen size and density. The system provides robust compatibility features that can handle most of the work of rendering your application on any device screen, provided that you‘ve implemented your UI using techniques that allow it to gracefully resize (as described in the Best Practices, below).
你不需要为每个屏幕尺寸和密度的组合提供多个资源。系统提供健壮的兼容特性可以处理在不同设备上展示你应用程序的大部分的工作,如果你已经用技巧实现了UI,系统会优雅的调整布局的大小。
Note: The characteristics that define a device‘s generalized screen size and density are independent from each other. For example, a WVGA high-density screen is considered a normal size screen because its physical size is about the same as the T-Mobile G1 (Android‘s first device and baseline screen configuration). On the other hand, a WVGA medium-density screen is considered a large size screen. Although it offers the same resolution (the same number of pixels), the WVGA medium-density screen has a lower screen density, meaning that each pixel is physically larger and, thus, the entire screen is larger than the baseline (normal size) screen.
注意:定义设备的广义尺寸和密度特性时是相互独立的,例如一个WVGA的高密度屏幕被定义为中级尺寸屏幕因为他的物理尺寸和G1(android的第一款手机,同时也是标准的屏幕配置)相同。另一方面,一个WVGA中密度屏幕是一个大屏幕手机。虽然它提供了相同的分辨率,但是WVGA中密度屏幕有更小的屏幕密度,这意味着每个像素物理上更大,因此这个屏幕就会比标准屏幕大。
Density independence
逻辑密度
Your application achieves "density independence" when it preserves the physical size (from the user‘s point of view) of user interface elements when displayed on screens with different densities.
当他保护你用户界面元素的物理尺寸在不同密度下显示时你的应用程序实现逻辑密度。
Maintaining density independence is important because, without it, a UI element (such as a button) appears physically larger on a low density screen and smaller on a high density screen. Such density-related size changes can cause problems in your application layout and usability. Figures 2 and 3 show the difference between an application when it does not provide density independence and when it does, respectively.
维护逻辑密度非常重要因为没有它UI元素(如一个按钮)低密度的屏幕上变大,在高密度的屏幕上变小,这些密度相关的尺寸变化可以导致你应用程序布局和可用性出现变化。图2和图3展示当一个应用程序没有提供逻辑密度和提供逻辑密度的不同。
Figure 2. Example application without support for different densities, as shown on low, medium, and high density screens.
Figure 3. Example application with good support for different densities (it‘s density independent), as shown on low, medium, and high density screens.
The Android system helps your application achieve density independence in two ways:
- The system scales dp units as appropriate for the current screen density
- The system scales drawable resources to the appropriate size, based on the current screen density, if necessary
android系统帮助你的应用程序通过下面两种方式实现逻辑密度:
系统会酌情缩放dp单位以适应当前的屏幕密度。
如果需要基于当前的屏幕密度系统缩放绘图资源到适当的尺寸。
In figure 2, the text view and bitmap drawable have dimensions specified in pixels (px
units), so the views are physically larger on a low density screen and smaller on a high density screen. This is because although the actual screen sizes may be the same, the high density screen has more pixels per inch (the same amount of pixels fit in a smaller area). In figure 3, the layout dimensions are specified in density-independent pixels (dp
units). Because the baseline for density-independent pixels is a medium-density screen, the device with a medium-density screen looks the same as it does in figure 2. For the low-density and high-density screens, however, the system scales the density-independent pixel values down and up, respectively, to fit the screen as appropriate.
图2,文本框和bitmap图片尺寸用像素设定,所以文本框在低密度上更大,在高密度更小,这个因为虽然实际屏幕尺寸可能相同但是高密度的屏幕每英寸有更高的像素。图3布局尺寸用dp设定,所以对于逻辑密度来说中级屏幕是标准配置,所以有中级配置的屏幕看上去和图2一样。对于低密度和高密度的屏幕来说系统缩放逻辑密度来适应屏幕大小。
In most cases, you can ensure density independence in your application simply by specifying all layout dimension values in density-independent pixels (dp
units) or with "wrap_content"
, as appropriate. The system then scales bitmap drawables as appropriate in order to display at the appropriate size, based on the appropriate scaling factor for the current screen‘s density.
However, bitmap scaling can result in blurry or pixelated bitmaps, which you might notice in the above screenshots. To avoid these artifacts, you should provide alternative bitmap resources for different densities. For example, you should provide higher-resolution bitmaps for high-density screens and the system will use those instead of resizing the bitmap designed for medium-density screens. The following section describes more about how to supply alternative resources for different screen configurations.
在大多数的情况下,你可以简单的通过用dp来设置所有布局用的尺寸或酌情用wrap_content来确保你的应用程序密度独立。系统酌情缩放图片资源为了显示适当的尺寸,基于当前屏幕密度进行适当的缩放因子。然而,位图缩放可以作用于模糊的位图和像素化的位图,你可能注意的屏幕的界面。为了避免这些图片的模糊,你可以提供多个位图资源为不同的密度,例如你可以提供高分辨率位图为高密度,系统将会用这些代替缩放为中密度而设计的位图。接下来的章节描述更多的关于如何用多资源来适应不同的屏幕配置。
How to Support Multiple Screens
怎么支持多屏幕
The foundation of Android‘s support for multiple screens is its ability to manage the rendering of an application‘s layout and bitmap drawables in an appropriate way for the current screen configuration. The system handles most of the work to render your application properly on each screen configuration by scaling layouts to fit the screen size/density and scaling bitmap drawables for the screen density, as appropriate. To more gracefully handle different screen configurations, however, you should also:
Android支持多屏幕的基础是他有能力管理布局和位图在当前屏幕以适当的方式展示。系统处理大多数的工作,酌情的通过缩放布局来适应屏幕的尺寸或密度酌情通过缩放位图来支持不同的屏幕密度,以这种方式把你的应用程序更好的显示在不同的屏幕配置上。为了更优雅的处理不同的屏幕配置你应该做如下:
- Explicitly declare in the manifest which screen sizes your application supports
- 清晰的在mainfest文件中定义你支持的屏幕尺寸
- By declaring which screen sizes your application supports, you can ensure that only devices with the screens you support can download your application. Declaring support for different screen sizes can also affect how the system draws your application on larger screens—specifically, whether your application runs in screen compatibility mode.
To declare the screen sizes your application supports, you should include the
<supports-screens>
element in your manifest file. - 通过定义你应用程序支持的屏幕尺寸,你可以确保只有你支持的屏幕设备才下载你的应用,定义支持不同屏幕尺寸也能影响你应用程序在巨屏上的如何呈现,尤其是你应用程序运行在屏幕兼容模式下。
- 你应该在mainfest文件的
<supports-screens>元素中定义你支持的屏幕尺寸。
- Provide different layouts for different screen sizes
- 为不同的屏幕尺寸提供不同的布局
- By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes. For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen.
The configuration qualifiers you can use to provide size-specific resources are
small
,normal
,large
, andxlarge
. For example, layouts for an extra large screen should go inlayout-xlarge/
.Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the
sw<N>dp
configuration qualifier to define the smallest available width required by your layout resources. For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it inlayout-sw600dp/
. Using the new techniques for declaring layout resources is discussed further in the section about Declaring Tablet Layouts for Android 3.2. - 默认情况下android会重绘你的布局来适应设备屏幕,在大多数情况下,工作良好,在某些情况下,你的UI可能看上去不ok并且可能需要为不同的屏幕尺寸调整。例如一个巨屏你可能想利用额外的屏幕空间通过调整一些控件的位置和尺寸,或者在一些小屏幕上,你可能需要调整尺寸来显示出所有的内容。 你能用配置修饰符
small
,normal
,large
, 和xlarge
提供指定尺寸资源。例如为超大屏准备的布局应该在layout-xlarge/下。 - 从Android3.2开始,反对用以上尺寸组应该用sw<>dp配置修饰符替代来定义最小的所需宽度通过你的布局文件,例如如果多窗体平板布局需要至少600dp屏幕宽度,你应该放在layout-sw600dp/下。在Declaring Tablet Layouts for Android 3.2.中会更多的讨论用新技术来定义布局文件。
- Provide different bitmap drawables for different screen densities
- 提供不同的位图为不同的屏幕密度
- By default, Android scales your bitmap drawables (
.png
,.jpg
, and.gif
files) and Nine-Patch drawables (.9.png
files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.The configuration qualifiers you can use for density-specific resources are
ldpi
(low),mdpi
(medium),hdpi
(high), andxhdpi
(extra high). For example, bitmaps for high-density screens should go indrawable-hdpi/
. - 默认情况下,Android缩放你的位图文件(
.png
,.jpg
, 和.gif
文件)和九宫格图片(.9.png文件)使得他们在每个设备上以是适当的尺寸显示。例如你的应用程序提供只为标准尺寸中级密度的屏幕提供位图,那么在高密度系统会放大位图,在低密度系统会缩小位图。这些缩放能导致图片模糊,为了确保你的图片效果最佳,你应该为不同的屏幕密度提供不同版本的资源文件。 - 你能为制定密度用的配置符是
ldpi
(低),mdpi
(中),hdpi
(高), 和xhdpi
(超高).例如为高密度屏幕准备的图片应该放在drawable-hdpi/下。
The size and density configuration qualifiers correspond to the generalized sizes and densities described inRange of screens supported, above.
Note: If you‘re not familiar with configuration qualifiers and how the system uses them to apply alternative resources, read Providing Alternative Resources for more information.
这个尺寸和密度配置符相当于广义的尺寸和密度在Range of screens supported中有描述。
注意:如果你不熟悉配置符并且不知道系统如果用这些资源,请阅读 Providing Alternative Resources 。
At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:
在运行时,系统为每个资源用下面的过程来确保在当前屏幕上有更好的显示效果:
- The system uses the appropriate alternative resource
Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the
hdpi
qualifier (such asdrawable-hdpi/
) might be the best match, so the system uses the drawable resource from this directory. - If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density
The "default" resources are those that are not tagged with a configuration qualifier. For example, the resources indrawable/
are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate.However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won‘t always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.
1.基于当前系统的尺寸和密度系统会选择适当的资源,系统会用应用程序提供任何指定尺寸和指定密度的文件。例如如果设备有高密度并且应用程序需要图片资源,系统会寻找和屏幕配置相匹配的资源文件。依赖其他可用资源,一个名字包括hdpi的资源文件夹(例如drawable-hdpi),可能会更好的匹配,所以系统会直接用这些文件夹。
2.如果没有匹配的资源可用,系统用默认的资源或者是根据当前屏幕配置进行缩放。默认的资源是没有配置符的,例如drawable/
的资源是默认资源。系统假定默认的资源是为标准屏幕尺寸和密度设计,标准屏幕尺寸和密度是中级尺寸和中级密度。系统会缩放资源为不同的屏幕配置。
然而,当系系统在没有找到指定密度的文件夹时,它不会总是用默认资源。当缩放时系统为了提供更好的结果,也许系统会使用其他的密度的资源。例如当寻找低密度的资源但没找到时,系统更喜欢缩小高密度的资源,因为系统能轻易的按照0.5的比例缩小高密度资源为低密度资源,能轻易的按照0.75的比例缩小高密度资源为中密度资源。
For more information about how Android selects alternative resources by matching configuration qualifiers to the device configuration, read How Android Finds the Best-matching Resource.
关于Android通过配置符来选择资源匹配设备配置的更多内容请看 How Android Finds the Best-matching Resource.
Using configuration qualifiers
用配置符
Android supports several configuration qualifiers that allow you to control how the system selects your alternative resources based on the characteristics of the current device screen. A configuration qualifier is a string that you can append to a resource directory in your Android project and specifies the configuration for which the resources inside are designed.
To use a configuration qualifier:
Android支持一些配置符,这些配置符通会让系统根据不同的屏幕选择不同的资源。配置符是一个字符串,这个字符串跟在资源文件夹名称的后面,为预先设计的资源指定配置。
用这些配置符可以做:
- Create a new directory in your project‘s
res/
directory and name it using the format:<resources_name>-<qualifier>
<resources_name>
is the standard resource name (such asdrawable
orlayout
).<qualifier>
is a configuration qualifier from table 1, below, specifying the screen configuration for which these resources are to be used (such ashdpi
orxlarge
).
You can use more than one
<qualifier>
at a time—simply separate each qualifier with a dash. - Save the appropriate configuration-specific resources in this new directory. The resource files must be named exactly the same as the default resource files.
1.在你的工程 res/
目录下创建一个新的文件夹并且用<resources_name>-<qualifier>格式命名。
<resources_name>是标准资源名字(如drawable
或 layout
)
<qualifier>是一个配置符从下面表1看,指定不同屏幕配置用的资源。
For example, xlarge
is a configuration qualifier for extra large screens. When you append this string to a resource directory name (such as layout-xlarge
), it indicates to the system that these resources are to be used on devices that have an extra large screen.
例如,xlarge是巨屏的配置符,当你在资源文件夹的名字后面xlarge(例如layout-xlarge)时,它告诉系统这个资源文件是为巨屏的设备准备的。
Table 1. Configuration qualifiers that allow you to provide special resources for different screen configurations.
Screen characteristic Qualifier Description
Size | small |
Resources for small size screens.小屏幕 |
normal |
Resources for normal size screens. (This is the baseline size.)中屏幕,标准尺寸 | |
large |
Resources for large size screens.大屏幕 | |
xlarge |
Resources for extra large size screens.巨屏幕 | |
Density | ldpi |
Resources for low-density (ldpi) screens (~120dpi).低密度 |
mdpi |
Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)中密度,标准密度 | |
hdpi |
Resources for high-density (hdpi) screens (~240dpi).高密度 | |
xhdpi |
Resources for extra high-density (xhdpi) screens (~320dpi).超高密度 | |
nodpi |
Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen‘s density. 为所有的密度,无乱屏幕什么密度,系统都不缩放资源。 |
|
tvdpi |
Resources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn‘t need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi. 这个资源是为在中密度和高密度质检的屏幕准备的,近似于213dpi,这不认为是主要的密度分类。这几乎是为电视准备,大多数的应用不需要他,mdpi和hdpi资源足够给大多数的应用并且系统会酌情缩放他们。如果你发现必须要提供tvdpi资源,则你应该按照1.33*mdpi的比例来提供,例如中密度下100px *100px图片,则tvdpi应该是133px*133px图片。 |
|
Orientation | land |
Resources for screens in the landscape orientation (wide aspect ratio).横屏 |
port |
Resources for screens in the portrait orientation (tall aspect ratio).竖屏 | |
Aspect ratio | long |
Resources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration. 比标准屏幕宽高比明显的高或者宽的这样屏幕。 |
notlong |
Resources for use screens that have an aspect ratio that is similar to the baseline screen configuration. 和标准屏幕配置一样的屏幕宽高比的屏幕。 |
Note: If you‘re developing your application for Android 3.2 and higher, see the section about Declaring Tablet Layouts for Android 3.2 for information about new configuration qualifiers that you should use when declaring layout resources for specific screen sizes (instead of using the size qualifiers in table 1).
注意:如果你为Android3.2或更高的应用开发,看Declaring Tablet Layouts for Android 3.2 章节关于当为特定屏幕开发你应该用的新的配置符。
For more information about how these qualifiers roughly correspond to real screen sizes and densities, seeRange of Screens Supported, earlier in this document.
关于这些配置符如何大致相当于真实的屏幕尺寸和密度,请看Range of Screens Supported,或更早的文档。
For example, the following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.
例如,下面的应用程序里资源文件夹列表为不同的屏幕尺寸提供了不同的布局,为不同的屏幕密度提供不同的图片资源。
res/layout/my_layout.xml // layout for normal screen size ("default") res/layout-small/my_layout.xml // layout for small screen size res/layout-large/my_layout.xml // layout for large screen size res/layout-xlarge/my_layout.xml // layout for extra large screen size res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation res/drawable-mdpi/my_icon.png // bitmap for medium density res/drawable-hdpi/my_icon.png // bitmap for high density res/drawable-xhdpi/my_icon.png // bitmap for extra high density
For more information about how to use alternative resources and a complete list of configuration qualifiers (not just for screen configurations), see Providing Alternative Resources.
Be aware that, when the Android system picks which resources to use at runtime, it uses certain logic to determing the "best matching" resources. That is, the qualifiers you use don‘t have to exactly match the current screen configuration in all cases in order for the system to use them. Specifically, when selecting resources based on the size qualifiers, the system will use resources designed for a screen smaller than the current screen if there are no resources that better match (for example, a large-size screen will use normal-size screen resources if necessary). However, if the only available resources are larger than the current screen, the system will not use them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the xlarge
qualifier, but the device is a normal-size screen). For more information about how the system selects resources, read How Android Finds the Best-matching Resource.
关于如何使用资源和配置符的完整清单,请看Providing Alternative Resources。
值得注意的是,当Android系统运行时选择哪个资源,它用某种逻辑来决定最佳匹配资源。也就是这个配置符不必在所有的情况完全匹配所有的屏幕尺寸。特别是当基于尺寸和密度选择资源文件时,如果这没有更佳的匹配系统会用比当前屏幕小的设计资源给当前屏幕(例如必要的话大屏幕会用中屏幕的资源)。然而如果唯一可用的资源比当前屏幕大,系统不会用他们并且如果没有其他资源匹配设备时你的应用会崩溃(例如,如果所有的布局文件为巨屏设计,但是当前设备却是中屏幕),更多信息关于系统如何选择资源的,请读How Android Finds the Best-matching Resource.
Tip: If you have some drawable resources that the system should never scale (perhaps because you perform some adjustments to the image yourself at runtime), you should place them in a directory with the nodpi
configuration qualifier. Resources with this qualifier are considered density-agnostic and the system will not scale them.
提示:如果你有图片资源系统系统永远不缩放(也许因为在运行时你自己的做一些图片调整),你应该把他们放在有nodpi为后缀的文件夹中。放到这个文件夹后就和系统的密度无关了并且系统也不会缩放他们了。
Designing alternative layouts and drawables
设计多布局和多图片资源
The types of alternative resources you should create depends on your application‘s needs. Usually, you should use the size and orientation qualifiers to provide alternative layout resources and use the density qualifiers to provide alternative bitmap drawable resources.
你应该依照你应用程序的需要来创建资源的类型,通常,你应该用的尺寸标识符和方向标识符来提供布局资源,用密度标识符提供图片资源。
The following sections summarize how you might want to use the size and density qualifiers to provide alternative layouts and drawables, respectively.
下面的章节总结了你可能为尺寸和密度提供的布局和图片资源。
Alternative layouts
可选布局
Generally, you‘ll know whether you need alternative layouts for different screen sizes once you test your application on different screen configurations. For example:
- When testing on a small screen, you might discover that your layout doesn‘t quite fit on the screen. For example, a row of buttons might not fit within the width of the screen on a small screen device. In this case you should provide an alternative layout for small screens that adjusts the size or position of the buttons.
- When testing on an extra large screen, you might realize that your layout doesn‘t make efficient use of the big screen and is obviously stretched to fill it. In this case, you should provide an alternative layout for extra large screens that provides a redesigned UI that is optimized for bigger screens such as tablets.
Although your application should work fine without an alternative layout on big screens, it‘s quite important to users that your application looks as though it‘s designed specifically for their devices. If the UI is obviously stretched, users are more likely to be unsatisfied with the application experience. - And, when testing in the landscape orientation compared to the portrait orientation, you might notice that UI elements placed at the bottom of the screen for the portrait orientation should instead be on the right side of the screen in landscape orientation.
通常,一旦你在不同的屏幕配置测试,你会知道是否你需要为不同的屏幕提供不同的布局。例如:
当在一个小屏幕上测试,你可能发现你的布局不是完全的适应屏幕,例如一排按钮可能在小屏幕的宽度上显示不完全,在这种情况下你应该为小屏幕提供一个调整过的按钮的尺寸和位置的可选的布局。
当在巨屏上测试时,你可能意识到你的布局不能有效的利用大屏幕并且为了填充屏幕被明显的拉伸了,在这种情况下,你应该为巨屏例如平板设备提供一个重新设计的更优的布局。
虽然你的应用程序在没有巨屏布局的情况下应该工作良好,但对于用户来说非常重要的是看上去你的应用程序为他们的设备经过了特别的设计。如果UI被拉伸了,用户可能更不满意用户体验。
并且,当在横屏比较竖屏测试时,你可能会注意到为竖屏设计的被放置在屏幕底部的UI元素在横屏情况下应该被放在右侧。
To summarize, you should be sure that your application layout:
- Fits on small screens (so users can actually use your application)
- Is optimized for bigger screens to take advantage of the additional screen space
- Is optimized for both landscape and portrait orientations
总结一下,你应该确保你的应用程序布局如下:
适合小屏幕(实际上用户能用你的应用程序)
利用额外的屏幕空间为大屏幕做优化。
支持横屏和竖屏
If your UI uses bitmaps that need to fit the size of a view even after the system scales the layout (such as the background image for a button), you should use Nine-Patch bitmap files. A Nine-Patch file is basically a PNG file in which you specific two-dimensional regions that are stretchable. When the system needs to scale the view in which the bitmap is used, the system stretches the Nine-Patch bitmap, but stretches only the specified regions. As such, you don‘t need to provide different drawables for different screen sizes, because the Nine-Patch bitmap can adjust to any size. You should, however, provide alternate versions of your Nine-Patch files for different screen densities.
如果你的UI需要用位图来填充一个控件的尺寸,甚至是在布局被缩放以后(例如按钮的景背图片)。你应该用九宫格图片文件,一个九宫格图片基本是上一个PNG文件,是一个你指定的两维区域可以被拉伸的PNG图片。当系统需要缩放带有位图的控件是,系统会拉伸九宫格图片,但是只拉伸指定区域,这种情况下你不需要为不同的屏幕尺寸提供不同的图片,所以九宫格图片能使用任何尺寸。然而你应该为不同屏幕密度提供不同的图片。
Alternative drawables
可选的图片
Figure 4. Relative sizes for bitmap drawables that support each density.
Almost every application should have alternative drawable resources for different screen densities, because almost every application has a launcher icon and that icon should look good on all screen densities. Likewise, if you include other bitmap drawables in your application (such as for menu icons or other graphics in your application), you should provide alternative versions or each one, for different densities.
几乎每个应用程序都应该为不同的屏幕密度提供不同的图片资源,因为几乎所有的应用都有一个桌面图标并且这个图标在所有的屏幕密度下都看着正常。同样的如果在你的应用程序你包括其他图片(例如为菜单图标和其他的图片)你应该为不同的密度提供不同版本的图片。
Note: You only need to provide density-specific drawables for bitmap files (.png
, .jpg
, or .gif
) and Nine-Path files (.9.png
). If you use XML files to define shapes, colors, or other drawable resources, you should put one copy in the default drawable directory (drawable/
).
注意:你只需要为位图文件(.png
, .jpg
, or .gif
)和(.9.png)提供指定密度资源。如果你用xml文件定义形状,颜色,或其他资源文件,你应该拷贝一份在默认的资源文件夹中(drawable/
)。
To create alternative bitmap drawables for different densities, you should follow the 3:4:6:8 scaling ratio between the four generalized densities. For example, if you have a bitmap drawable that‘s 48x48 pixels for medium-density screen (the size for a launcher icon), all the different sizes should be:
- 36x36 for low-density
- 48x48 for medium-density
- 72x72 for high-density
- 96x96 for extra high-density
你应该按照3:4:6:8的比例为不同的密度创建不同的图片资源,例如你有一个位图是48*48像素的为中密度屏幕提供的(桌面图标的尺寸),所有不同的尺寸应该是:
- 36x36 低密度
- 48x48 中密度
- 72x72 高密度
- 96x96 超高密度
For more information about designing icons, see the Icon Design Guidelines, which includes size information for various bitmap drawables, such as launcher icons, menu icons, status bar icons, tab icons, and more.
关于设计图标的信息请看Icon Design Guidelines,这有不同的图片资源尺寸信息,例如桌面图标,按钮图标,状态烂图标,tab图标等。
Declaring Tablet Layouts for Android 3.2
为Android3.2定义布局
For the first generation of tablets running Android 3.0, the proper way to declare tablet layouts was to put them in a directory with the
xlarge
configuration qualifier (for example, res/layout-xlarge/
). In order to accommodate other types of tablets and screen sizes—in particular, 7" tablets—Android 3.2 introduces a new way to specify resources for more discrete screen sizes. The new technique is based on the amount of space your layout needs (such as 600dp of width), rather than trying to make your layout fit the generalized size groups (such as large or xlarge).
The reason designing for 7" tablets is tricky when using the generalized size groups is that a 7" tablet is technically in the same group as a 5" handset (the large group). While these two devices are seemingly close to each other in size, the amount of space for an application‘s UI is significantly different, as is the style of user interaction. Thus, a 7" and 5" screen should not always use the same layout. To make it possible for you to provide different layouts for these two kinds of screens, Android now allows you to specify your layout resources based on the width and/or height that‘s actually available for your application‘s layout, specified in dp units.
第一带平板运行的是Android3.0系统,更好的定义平板布局的方式是把他们放到带xlarge配置符的文件夹中(例如,res/layout-xlarge/)。为了适应其他平板类型和7寸的屏幕平板类型,Android3.2介绍了一个新的为更多屏幕尺寸指定资源的方法。新的技术是基于你布局需要的空间总数(例如600dp宽),而不是尝试让你的布局填充广义的尺寸组(像大屏幕和巨屏)。
设计7寸屏幕复杂的原因是当用广义的尺寸组是技术上7寸平板和5寸的手机用相同的尺寸组(巨屏组)。同时这两个设备在尺寸上比较接近,作为用户互动的方式对于应用程序的UI所用的空间是明显不同的。因此7寸和5寸屏幕应该用不同的布局,为了为这两种屏幕提供不同的布局成为可能,Android现在允许你用dp单位指定你布局文件在屏幕上可用的的宽或高。
For example, after you‘ve designed the layout you want to use for tablet-style devices, you might determine that the layout stops working well when the screen is less than 600dp wide. This threshold thus becomes the minimum size that you require for your tablet layout. As such, you can now specify that these layout resources should be used only when there is at least 600dp of width available for your application‘s UI.
例如,在你为平板设备提供设计布局后,你可决定在小于600dp的屏幕上停止使用这个布局。这个限制因此变成你平板布局的最小尺寸。在这种情况下,你能现在指定这些布局资源应该被用在至少是600dp宽的应用布局上。
You should either pick a width and design to it as your minimum size, or test what is the smallest width your layout supports once it‘s complete.
你应该选择一个宽度设计作为你的最小尺寸,或者一旦设计完成测试你布局支持的最小宽度。
Note: Remember that all the figures used with these new size APIs are density-indpendent pixel (dp) values and your layout dimensions should also always be defined using dp units, because what you care about is the amount of screen space available after the system accounts for screen density (as opposed to using raw pixel resolution). For more information about density-indpendent pixels, read Terms and concepts, earlier in this document.
注意:记住,所有使用这些新的尺寸数据API是逻辑密度像素(dp)值并且你的布局尺寸也应该用用dp单位定义。因为你所关心的是计算屏幕密度后的可用空间(而不是使用原始像素分辨率)。更多关于逻辑密度的请看Terms and concepts,或更早的文档。
Using new size qualifiers
用新的尺寸修饰符
The different resource configurations that you can specify based on the space available for your layout are summarized in table 2. These new qualifiers offer you more control over the specific screen sizes your application supports, compared to the traditional screen size groups (small, normal, large, and xlarge).
表2已经汇总了能按照可用空间的方式为不同布局
你能基于可用空间的方式指定不同的资源配置应汇总在表2中,相比较传统的屏幕尺寸组而然(小,中,大,巨大)这些新的标识符为你的应用程序支持特定的屏幕尺寸上提供更多的控制。
Note: The sizes that you specify using these qualifiers are not the actual screen sizes. Rather, the sizes are for the width or height in dp units that are available to your activity‘s window. The Android system might use some of the screen for system UI (such as the system bar at the bottom of the screen or the status bar at the top), so some of the screen might not be available for your layout. Thus, the sizes you declare should be specifically about the sizes needed by your activity—the system accounts for any space used by system UI when declaring how much space it provides for your layout. Also beware that the Action Bar is considered a part of your application‘s window space, although your layout does not declare it, so it reduces the space available for your layout and you must account for it in your design.
注意:用这些标识符指定的尺寸不是实际的屏幕尺寸。相反,这用dp单位定义的宽或高对于你的界面是可用的。Android系统可能用一些系统UI(像在屏幕底部的系统栏或在顶部状态栏),所以一些屏幕对于你布局可能不可用。因此你定义的尺寸应该被特指是你activity所需要的尺寸-系统计算系统的UI任何尺寸,当定义为你布局提供多少空间时。也当心action bar被认为是你应用程序界面的一部分,虽然你的布局没有定义action bar。所以你应该为你的布局减去这部分空间并且在你的设计里应该计算它。
Table 2. New configuration qualifers for screen size (introduced in Android 3.2).
表2为屏幕尺寸新的配置符(Android3.2引进的)
Screen configuration Qualifier values Description
smallestWidth | sw<N>dp
Examples: |
The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device‘s smallestWidth is the shortest of the screen‘s available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen‘s current orientation, your application‘s has at least For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifer to create the layout resources, 屏幕的基础尺寸,就像可用屏幕区域的最短尺寸,尤其是设备的最小宽度是屏幕可用高度或宽度的最短尺寸(你可能会想它是屏幕最小可能宽度)。你能用这个标识符确保,无论屏幕的当前方向,你的应用程序宽度至少有<N>dps可用。例如,如果你的布局需要屏幕区域上的最小尺寸在任何时候至少是600dp,则你能用这个标识符创建布局资源res/layout-sw600dp/只有当屏幕的最小宽度是600dp时系统才会用这些资源文件,无论600dp是用户认为的宽或高。最小屏幕宽度是固定的设备特有的屏幕尺寸;当屏幕的方向改变时设备的最小宽度不变。 The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI. 设备的最小宽度要考虑屏幕的尺寸和系统UI,例如,如果在屏幕上设备有一些持久化UI元素沿着最小宽度轴线计算空间,系统声明的最小宽度会比实际的屏幕尺寸小,因为这些屏幕像素对于UI来说不可用。 This is an alternative to the generalized screen size qualifiers (small, normal, large, xlarge) that allows you to define a discrete number for the effective size available for your UI. Using smallestWidth to determine the general screen size is useful because width is often the driving factor in designing a layout. A UI will often scroll vertically, but have fairly hard constraints on the minimum space it needs horizontally. The available width is also the key factor in determining whether to use a one-pane layout for handsets or multi-pane layout for tablets. Thus, you likely care most about what the smallest possible width will be on each device. 这个关于广义的屏幕尺寸标识符(小,中,大,巨大)为有效的尺寸允许你定义一个连续的数字。用最小的宽度决定广义屏幕尺寸是有用的因为宽度经常做除数在布局设计时。一个UI会经常垂直滚动,但是在水平方向上会适应的硬件约束需要的最小空间。可用宽度是决定为手机用一个布局关键因素,还是为平板用多个布局,因此,你关心每个设备上的最小宽度。 |
Available screen width | w<N>dp
Examples: |
Specifies a minimum available width in dp units at which the resources should be used—defined by the This is often useful to determine whether to use a multi-pane layout, because even on a tablet device, you often won‘t want the same multi-pane layout for portrait orientation as you do for landscape. Thus, you can use this to specify the minimum width required for the layout, instead of using both the screen size and orientation qualifiers together. |
Available screen height | h<N>dp
Examples: |
Specifies a minimum screen height in dp units at which the resources should be used—defined by the Using this to define the height required by your layout is useful in the same way as |