Android官方文档翻译 十四 3.2Supporting Different Screens

偶尔善良 提交于 2020-03-25 00:08:58

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
    图像设计向导

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)

    一般有四种密度:低(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 - suffix. For example, a unique layout for large screens should be saved under res/layout-large/.

为了在不同的屏幕尺寸上优化你的用户体验,你应该为你想要支持的每种屏幕尺寸都创建一份独一无二的布局XML文件。每个布局里应该保存进相应的资源目录,以作为后缀来命名。例如,对于大屏幕的一份独特布局应该被保存在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:

通常在你的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.

系统会根据你的应用程序所运行的设备的屏幕大小从相应的布局目录中加载布局文件。Providing Resources向导中提供了更多关于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.

默认情况下,/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.

这意味着如果你要为极高密度的设备生成一个200×200的图像,你应该为后面这些生成相同的资源:对于hdpi设备生成150×150,对于mdpi设备生成100×100,对于ldpi设备生成75×75。

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 disign向导。

Next: Supporting Different Platform Versions

下一节:支持不同平台版本

这是我自己翻译的,如果您发现其中有重要错误,敬请指出,感谢!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!