Android官方入门文档[10]支持不同的屏幕

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

时间: 2024-09-12 14:05:33

Android官方入门文档[10]支持不同的屏幕的相关文章

Android官方入门文档[9]支持不同的语言

Android官方入门文档[9]支持不同的语言 Supporting Different Languages支持不同的语言   This class teaches you to1.Create Locale Directories and String Files2.Use the String Resources You should also read•Localization Checklist•Localization with Resources 该课程教你1.创建区域设置目录和文件

Android官方入门文档[11]支持不同平台版本

Android官方入门文档[11]支持不同平台版本 Supporting Different Platform Versions支持不同平台版本   This lesson teaches you to1.Specify Minimum and Target API Levels2.Check System Version at Runtime3.Use Platform Styles and Themes You should also read•Android API Levels•Andr

Android官方入门文档[4]启动另一个Activity

Android官方入门文档[4]启动另一个Activity   Starting Another Activity启动另一个Activity This lesson teaches you to1.Respond to the Send Button2.Build an Intent 3.Create the Second Activity4.Receive the Intent5.Display the Message You should also read•Installing the S

Android官方入门文档[7]样式化操作栏

Android官方入门文档[7]样式化操作栏   Styling the Action Bar样式化操作栏   This lesson teaches you to1.Use an Android Theme2.Customize the Background3.Customize the Text Color4.Customize the Tab Indicator You should also read•Styles and Themes•Android Action Bar Style

Android官方入门文档[16]创建一个Fragment代码片段

Android官方入门文档[16]创建一个Fragment代码片段   Creating a Fragment创建一个Fragment代码片段   This lesson teaches you to1.Create a Fragment Class2.Add a Fragment to an Activity using XML You should also read•Fragments 这节课教你1.创建一个Fragment代码片段类2.使用XML来添加一个Fragment代码片段给一个活

Android官方入门文档[1]创建一个Android项目

Android官方入门文档[1]创建一个Android项目   创建一个Android项目   这节课教你1.创建与Android Studio中的一个项目2.创建使用命令行工具项目 你也应该阅读•管理项目 Creating an Android Project This lesson teaches you to1.Create a Project with Android Studio2.Create a Project with Command Line Tools You should

Android官方入门文档

Android官方入门文档   欢迎来到为Android开发者的培训.在这里,你会发现套课中,描述了如何实现代码示例中的应用程序,你可以重复使用一个特定的任务类.类被组织成可以在左侧导航的顶层看到几个组.这第一组,入门,教你最基本的Android应用程序的开发.如果你是一个新的Android应用程序开发者,你应该按照顺序完成这些课程. 如果你认为你可能更喜欢通过互动视频培训学习的基础知识,看看这个预告片在Android开发的基础的一门课程. Getting Started   Welcome t

Android官方入门文档[5]建立操作栏

Android官方入门文档[5]建立操作栏   Setting Up the Action Bar建立操作栏 This lesson teaches you to1.Support Android 3.0 and Above Only2.Support Android 2.1 and Above You should also read•Setting Up the Support Library 这节课教你1.仅支持Android3.0及以上2.支持Android2.1及以上 你也应该阅读•设

Android官方入门文档[17]构建灵活的UI

Android官方入门文档[17]构建灵活的UI Building a Flexible UI构建灵活的UI   This lesson teaches you to1.Add a Fragment to an Activity at Runtime2.Replace One Fragment with Another You should also read•Fragments•Supporting Tablets and Handsets这节课教你1.在运行时新增一个片段给一个活动2.用另一