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

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

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

Android官方文档翻译 十四 3.2Supporting Different Screens的更多相关文章

  1. Android官方文档翻译 十六 4.Managing the Activity Lifecycle

    Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...

  2. Android官方文档翻译 十五 3.3Supporting Different Platform Versions

    Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...

  3. Android官方文档翻译 十二 3.Supporting Different Devices

    Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...

  4. Android官方文档翻译 十 2.3Styling the Action Bar

    Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...

  5. Android官方文档翻译 十八 4.2Pausing and Resuming an Activity

    Pausing and Resuming an Activity 暂停和恢复一个activity This lesson teaches you to 这节课教给你 Pause Your Activi ...

  6. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  7. Android官方文档翻译 四 1.2Running Your App

    Running Your App If you followed the previous lesson to create an Android project, it includes a def ...

  8. Android笔记二十四.Android基于回调的事件处理机制

        假设说事件监听机制是一种托付式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器全然消失了,当用户在GUI控件上激发某个事件时,控 ...

  9. <Android 基础(十四)> selector

    介绍 A StateListDrawable is a drawable object defined in XML that uses a several different images to r ...

随机推荐

  1. CF975A Aramic script 题解

    Content 定义一个字符串的根为字符串中不同种类的字符按字典序非降序排列得到的字符串.例如 \(\texttt{aaa}\) 的词根为 \(\texttt{a}\),\(\texttt{babb} ...

  2. 几个主流TCP/IP协议栈介绍

    我们知道TCP IP协议栈内包括了诸多协议.那么对于这当中的协议的功能以及作用,我们来具体了解一下吧.现在让我们做一个盘点,帮助大家总结一下,还望对大家能够有所帮助. 1.BSD TCP IP协议栈 ...

  3. 关于c++、go、nodejs、python的计算性能测试,结果令人惊讶

    计算性能在计算密集型的服务上,是非常重要的, 一直以为,在计算性能上,肯定是C++  > go > nodejs >= python 但测试结果却让人大跌眼镜!!! 实际的结果是: ...

  4. SpringBoot简单整合分布式任务调度平台(XXL-JOB)

    官方文档:https://www.xuxueli.com/xxl-job/#%E3%80%8A%E5%88%86%E5%B8%83%E5%BC%8F%E4%BB%BB%E5%8A%A1%E8%B0%8 ...

  5. NLTK 3.2.5 documentation Installing NLTK

    Installing NLTK NLTK requires Python versions 2.7, 3.4, or 3.5 Mac/Unix Install NLTK: run sudo pip i ...

  6. 一个在线MP4提取mp3的网站

    网址 https://airmore.cn/extract-audio-online

  7. qt5之使用QtXlsxWriter库

    note Qt version: 5.12 platform: os x 10.15 本文将介绍直接使用QtXlsxWriter源码 准备 下载QtXlsxWriter 使用Qt Creator 创建 ...

  8. 1114. Boxes

    1114. Boxes Time limit: 0.6 secondMemory limit: 64 MB N boxes are lined up in a sequence (1 ≤ N ≤ 20 ...

  9. 面渣逆袭:JVM经典五十问,这下面试稳了!

    大家好,我是老三,"面渣逆袭"系列继续,这节我们来搞定JVM.说真的,JVM调优什么的一个程序员可能整个职业生涯都碰不到两次,但是,一旦用到的时候,那就是救命了,而且最重要的是-- ...

  10. Java代码性能优化

    (1)在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 控制资源的使用,通过线程同步来控制资源的并 ...