[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
    评论
智慧校园建设方案旨在通过信息化手段提升教育、管理和服务水平,实现资源数字化、工作流程化、管理高效化和决策智能化。方案包括智慧校园信息化平台和安防平台的建设,涉及教学、科研、管理和服务等多个方面,以满足现代教育和培训需求。 技术服务要求强调了统一支撑平台的建设,包括数据标准、接口标准、代码标准和用户信息标准的统一制定。平台需满足信创和X86交叉适配要求,确保安全自主可控的系统开发环境。此外,方案还涵盖了用户中心系统、统一认证授权中心、统一工作流中心、统一智能报表中心等多个模块,以及数据共享中心、语音识别、移动服务终端等功能,以实现校园内外部信息的互联互通和资源共享。 智慧校园信息化平台的建设还包括了对教学管理、人事管理、公文管理、档案管理、即时通讯、会议管理、督办工作、资产管理等方面的数字化和自动化升级。这些模块的集成旨在提高工作效率,优化资源配置,加强监督管理,并通过移动应用等技术手段,实现随时随地的信息访问和业务处理。 安防平台的建设则侧重于校园安全,包括停车场管理、人脸识别测温、访客自助登记、视频监控等多个系统。这些系统的集成旨在提高校园的安全管理水平,实现对校园内外人员和车辆的有效监控和管理,确保校园环境的安全稳定。 最后,方案还提到了对固定资产的管理,包括购置、使用、归还、报废等全生命周期的管理,以及对网络设备、安防设备、服务器等硬件设施的配置和管理。通过这些措施,智慧校园建设方案旨在为校园提供一个安全、高效、便捷的学习和工作环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值