ConstraintLayout将边距应用于视图底部,也将其应用于视图的顶部

时间:2018-03-31 02:01:06

标签: java android android-layout android-studio android-constraintlayout

我有TextView,低于EditText,低于TextView。当我向EditText应用12 dp的下边距时,它已应用,但它也适用于视图的顶部。我不知道这是ConstraintLayout的工作方式。 marginTop按预期工作。

https://i.stack.imgur.com/VEOUz.jpg

1 个答案:

答案 0 :(得分:0)

从我在你的附件中看到的你创建一个垂直链但你并没有将你的“描述”限制在底部。这可能会导致问题,因为约束布局不知道链的结束位置。确保在描述TextView上添加app:layout_constraintBottom_toBottomOf="parent"或任何您需要的视图作为底链约束。

我重新创建了您的示例,并且编辑文本有镜像边距。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    app:layout_constraintBottom_toTopOf="@id/editText"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="spread" />

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="edit text"
    app:layout_constraintBottom_toTopOf="@+id/description"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/name" />

<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Description"
    android:textColor="@color/colorTextPrimaryLight"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

您可以使用chainStyle来查看最适合您的需求。