Android官方入门文档[10]支持不同的屏幕
Supporting Different Screens
支持不同的屏幕
This lesson teaches you to
1.Create Different Layouts
2.Create Different Bitmaps
You should also read
?Designing for Multiple Screens
?Providing Resources
?Iconography design guide
这节课教你
1.创建不同的布局
2.创建不同的位图
你也应该阅读
?设计为多屏幕
?提供资源
?影像学设计指南
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.
?There are four generalized sizes: small, normal, large, xlarge
?And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)
Android的分类使用两个常规属性设备屏幕:大小和密度。你应该预料到你的应用程序将与屏幕,范围在规模和密度的设备安装。因此,你应该包括优化您的应用程序的外观不同的屏幕大小和密度的一些替代资源。
?有四种通用尺寸:small, normal, large, xlarge(小,正常,大,超大)
?与4广义密度:低(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.
另外要注意,屏幕方向(横向或纵向)被认为是屏幕尺寸的变化,因此许多应用程序应该修改的布局,以优化每个方向的用户体验。
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.
文件名必须是完全一样的,但它们的内容是为了提供用于相应的屏幕尺寸优化的用户界面不同。
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如何选择合适的资源的更多信息,请在提供资源指南。
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及以上支持定义屏幕尺寸的先进方法,它允许您根据密度无关像素方面的最小宽度和高度,以指定的屏幕大小的资源。这节课不包括这种新技术。欲了解更多信息,请阅读设计的多个屏幕。
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
产生这些图像,你应该从你的矢量格式的原始资源,产生了使用以下尺寸的规模每次密度图像:
?xhdpi:2.0
?hdpi:1.5
?mdpi:1.0(基线)
?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.
这意味着,如果你生成一个200x200的图像xhdpi设备,你应该生成ldpi设备同一资源在150×150的hdpi,100×100的mdpi和75x75。
Then, place the files in the appropriate drawable resource directory:
然后,将在适当的绘制资源目录中的文件:
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.
对于更多的技巧和有关您的应用程序创建的图标资产的指引,看到影像学设计指南。
本文翻译自:https://developer.android.com/training/basics/supporting-devices/screens.html