如何用约束布局模仿加权LinearLayout [英] How to mimick Weighted LinearLayout with a Constraint Layout

查看:102
本文介绍了如何用约束布局模仿加权LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何像LinearLayout一样平均地约束空间中的空间?
例如,如果使用约束编写下面的布局,会变成怎样?

How can we spare space equally in a Constraint Layout as in LinearLayout?
For instance, how would the below layout become if it was written with constraints?

<LinearLayout 
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <TextView
    android:id="A"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

  <TextView
    android:id="B"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

  <TextView
    android:id="C"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

  <TextView
    android:id="D"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
</LinearLayout>

在约束布局中,我可以将AD设置为边缘,将A←B→D的偏移设置为33,将A←C→D的偏移设置为66,以使每个元素之间具有相同的间距. 但是,该解决方案并不能真正扩展规模.

In a Constraint Layout, I could set A and D to the edges, A←B→D with a 33 bias and A←C→D with a 66 bias to kind of have equal space between each element.
That solution does not really scale though.

在约束布局中有正确的方法吗?

Is there a proper way to do this in Constraint Layout?

推荐答案

FYI-约束布局alpha 9 添加了<一个href ="https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#Chains">链,可让您实现此行为.

FYI -- Constraint Layout alpha 9 added Chains, which allow you to implement this behavior.

这篇关于如何用约束布局模仿加权LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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