[Andriod官方训练教程]支持不同的设备之支持不同的屏幕

原文地址:https://developer.android.com/training/basics/supporting-devices/screens.html

---------------------------------------------------------------------------------------------------------------------------

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.

Android使用两个通用的性质来为设备屏幕进行分类:大小和密度。你应该期望你的app可以在一定范围内的大小和密度的设备上都可以安装。同样的,你应该包含一些可供选择的资源来为不同的屏幕大小和密度来完善你的app外观。

  • 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.

你同样要意识到,屏幕的方向(横向或纵向)也是一种屏幕大小,因此很多app应该修正布局来完善每个方向上的用户体验。

 

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 underres/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元素的绝对大小,而应该关注布局结构,这会影响到用户体验(例如重要的views相对于其他views的大小或位置)。

For example, this project includes a default layout and an alternative layout forlarge screens:

例如,这个项目包含了一个默认布局和一个供选择的为large屏幕提供的布局:

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 theProviding Resources guide.

系统根据你的app运行在什么样的屏幕大小来决定从哪个合适的布局目录下加载布局文件。更多有关Android怎样选择合适的有用资源的信息,请详见Providing Resources 指南。

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 thelarge 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.

你应该总是为每个通用的密度类型提供可以合适缩放的位图:low,medium,high和extra-high 密度。这可以帮助你在所有的屏幕密度上有很好的图片质量和性能。

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.

这意味着,如果你为xhdpi设备生成一张200X200的图片,你应该为hdpi、mdpi、ldpi分别生成150X150、100X100、75X75的相同的资源

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.

更多的关于为你的app创建图标资源的技巧和指南,参见Iconography design guide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 智慧社区背景与挑战 随着城市化的快速发展,社区面临健康、安全、邻里关系和服务质量等多方面的挑战。华为技术有限公司提出智慧社区解决方案,旨在通过先进的数字化技术应对这些问题,提升城市社区的生活质量。 2. 技术推动智慧社区发展 技术进步,特别是数字化、无线化、移动化和物联化,为城市社区的智慧化提供了可能。这些技术的应用不仅提高了社区的运行效率,也增强了居民的便利性和安全性。 3. 智慧社区的核心价值 智慧社区承载了智慧城市的核心价值,通过全面信息化处理,实现对城市各个方面的数字网络化管理、服务与决策功能,从而提升社会服务效率,整合社会服务资源。 4. 多层次、全方位的智慧社区服务 智慧社区通过构建和谐、温情、平安和健康四大社区模块,满足社区居民的多层次需求。这些服务模块包括社区医疗、安全监控、情感沟通和健康监测等。 5. 智慧社区技术框架 智慧社区技术框架强调统一平台的建设,设立数据中心,构建基础网络,并通过分层建设,实现平台能力及应用的可持续成长和扩展。 6. 感知统一平台与服务方案 感知统一平台是智慧社区的关键组成部分,通过统一的RFID身份识别和信息管理,实现社区服务的智能化和便捷化。同时,提供社区内外监控、紧急救助服务和便民服务等。 7. 健康社区的构建 健康社区模块专注于为居民提供健康管理服务,通过整合医疗资源和居民接入,实现远程医疗、慢性病管理和紧急救助等功能,推动医疗模式从治疗向预防转变。 8. 平安社区的安全保障 平安社区通过闭路电视监控、防盗报警和紧急求助等技术,保障社区居民的人身和财产安全,实现社区环境的实时监控和智能分析。 9. 温情社区的情感沟通 温情社区着重于建立社区居民间的情感联系,通过组织社区活动、一键呼叫服务和互帮互助平台,增强邻里间的交流和互助。 10. 和谐社区的资源整合 和谐社区作为社会资源的整合协调者,通过统一接入和身份识别,实现社区信息和服务的便捷获取,提升居民生活质量,促进社区和谐。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值