我也在试图弄清这个问题。我发现一种方法是选择两个视图,然后右键单击并选择水平居中。这样会创建链,但是您还必须相应地调整其他约束条件。我对Android是新手,所以肯定会有其他方法...
我遇到了同样的问题。按照教程所指的进入XML进行操作来解决它:
https://developer.android.com/training/basics/firstapp/building-ui.html在教程中,点击“查看最终的布局XML”并进行比较。我的XML文件缺少如下内容:
app:layout_constraintLeft_toRightOf="@+id/editText"
之后,我进入代码并手动将连接放在左侧元素中。
app:layout_constraintLeft_toRightOf="@+id/left_element"
然后就创建了链。 元素链接的图像
希望这有所帮助,抱歉没有发布图片,我的声誉还不够高XD。
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
同时,我们也可以在XML中手动声明链式样式,如下所示:
layout_constraintHorizontal_chainStyle或layout_constraintVertical_chainStyle
CHAIN_SPREAD -- 元素将会被平均分布(默认样式)
加权链式 -- 在CHAIN_SPREAD模式下,如果某些小部件设置为MATCH_CONSTRAINT,则它们将分配可用空间
CHAIN_SPREAD_INSIDE -- 类似于CHAIN_SPREAD,但是链的端点不会分散
CHAIN_PACKED -- 链的元素将被紧密排列在一起。然后,子级的水平或垂直偏差属性将影响紧密排列元素的位置
希望Android Studio编辑器能够改进这一点。
通过在编辑文本(app:layout_constraintRight_toLeftOf =“@ + id / button”)和按钮(app:layout_constraintBaseline_toBaselineOf =“@ + id / editText”)中添加约束解决
完整示例如下所示
<EditText
android:id="@+id/editText"
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:ems="10"
app:layout_constraintRight_toLeftOf="@+id/button"
android:hint="@string/edit_message"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/editText"
app:layout_constraintLeft_toRightOf="@+id/editText"
android:layout_marginLeft="16dp" />
我违反了教程,打开了自动连接(因为我想尝试一切)。
我选择了两个小部件,然后选择了水平居中。链被创建了,我关闭了自动连接,然后继续教程。