使用ConstraintsLayouts设计不同的屏幕尺寸 [英] Designing for different screen sizes using ConstraintsLayouts

查看:126
本文介绍了使用ConstraintsLayouts设计不同的屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始深入研究今年在IO上宣布的Constraints Layout。这看起来很有前途,使您的布局非常有效。我见过Google提供的codelab excersize。但是,我有以下问题需要解决。

I have started to dive into the Constraints Layout that were announced on the IO this year. This looks very promising and makes your layouts very efficient. I have seen the codelab excersize provided by Google. However I have the following questions that I would like to resolve.


  1. 如何使用 ConstraintLayouts 来设计不同屏幕尺寸的布局?

  1. How can ConstraintLayouts be used to design the layout for different screen sizes?

目前我设计布局并为特定设备/屏幕组设置视图的边距和填充等。将这些值保存在 dimens.xml 文件中。这是设计布局的一种非常痛苦的方法。在为多个屏幕设计时,Android ConstraintsLayout 是否模仿iOS的 AutoConstraintsLayout

Currently I design the layout and set the views' margins and paddings etc for a specific device/screen group. Save those values in the dimens.xml file. This is a REALLY painful method to design the layouts. Does the Android ConstraintsLayout mimic the AutoConstraintsLayout of iOS in designing for multiple screens as well?


  1. 为不同的屏幕方向设计布局是否仍然需要不同的Layout.xml文件,即; layout_landscape.xml layout_portraint.xml

  1. Does designing the Layout for different screen orientation still need different Layout.xml files i-e; layout_landscape.xml and layout_portraint.xml?


推荐答案

是的,ConstraintLayout的整个要点(以及重要的是,自Android 1.0以来的任何Android布局)都能够处理不同的屏幕(尺寸,密度......)。您没有什么特别的事情要做(除了指定小部件之间的约束)。

Yes, the entire point of ConstraintLayout (and for that matters, any Android layout since Android 1.0) is to be able to handle different screens (dimension, density...). You have nothing special to do (other than specifying constraints between widgets).

如果您想设计不同的屏幕尺寸,请将边距视为最小间距;并在窗口小部件之间创建约束,以便相对于其他窗口小部件定位窗口小部件。

If you want to design for different screen sizes, think about margins as "minimum" spacing; and create constraint between widgets so that you position widget relative to other widgets.

您还有指导和偏向中心,这为您提供了很大的灵活性。

You also have guidelines and bias centering, which give you a lot of flexibility.

关于屏幕方向,完全取决于您 - 如果不执行任何操作,您定义的布局将应用于任一方向。所以,它会奏效。但通常,您需要在纵向和横向之间使用不同的 UI。在这种情况下,您确实可以创建两个布局文件,通常的系统会为您选择合适的布局文件。

Regarding the screen orientation, it is entirely up to you -- if you do nothing, the layout you define will be applied on either orientation. So, it would "work". But often, you want a different UI between portrait and landscape. In that case, you can indeed create two layout files, and the usual system will pick the right one for you.

最后,您可以轻松地测试布局的方式通过从Android Studio中的弹出窗口更改当前屏幕定义,处理工作将在不同的屏幕/设备中运行:

Finally, you can easily test how the layout you are working on would behave in a different screen/device by changing the current screen definition from the popup in Android Studio:

您还可以使用调整大小手柄查看任意屏幕尺寸的布局。

You can also use the resize handle to see the layout to an arbitrary screen dimension.

这篇关于使用ConstraintsLayouts设计不同的屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆