如何使 ConstraintLayout 使用百分比值? [英] How to make ConstraintLayout work with percentage values?
问题描述
在 Android Studio 2.2 预览版 1 中,Google 在其支持库中发布了一个新布局:ConstraintLayout
.使用 ConstraintLayout 可以更轻松地在 Android Studio 中使用设计工具,但我没有找到使用相对大小(如 LinearLayout 中的百分比或权重")的方法.有没有办法根据百分比定义约束?例如.让一个视图占据屏幕的 40%,在视图之间创建 20% 的边距,将一个视图的宽度设置为另一个视图宽度的 50%?
With a Preview 1 of Android Studio 2.2 Google released a new layout in its support library: ConstraintLayout
. With ConstraintLayout it is easier to use a Design tool in Android Studio, but I didn't find a way to use relative sizes (percents or 'weights' like in LinearLayout). Is there a way to define the constraints based on percents? E.g. make a View take 40% of a screen, create 20% margin between views, set a View's width to 50% of another View's width?
推荐答案
您目前可以通过多种方式执行此操作.
You can currently do this in a couple of ways.
一是创建辅助线(右键单击设计区域,然后单击添加垂直/水平辅助线).然后,您可以单击指南的标题"以将定位更改为基于百分比.最后,您可以将视图限制为指南.
One is to create guidelines (right-click the design area, then click add vertical/horizontal guideline). You can then click the guideline's "header" to change the positioning to be percentage based. Finally, you can constrain views to guidelines.
另一种方法是使用偏差(百分比)定位视图,然后将其他视图锚定到该视图.
Another way is to position a view using bias (percentage) and to then anchor other views to that view.
也就是说,我们一直在考虑如何提供基于百分比的维度.我无法做出任何承诺,但这是我们想要添加的内容.
That said, we have been thinking about how to offer percentage based dimensions. I can't make any promise but it's something we would like to add.
这篇关于如何使 ConstraintLayout 使用百分比值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!