【问题标题】:ConstraintLayout applying margin bottom to a view, also applies it to the top of the viewConstraintLayout 将边距底部应用于视图,也将其应用于视图的顶部
【发布时间】:2018-03-31 02:01:06
【问题描述】:

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

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

【问题讨论】:

  • 你能显示你的xml代码吗?
  • 兄弟,我刚刚添加了截图,请看一下

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


【解决方案1】:

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

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

<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,看看哪种最适合您的需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2020-06-07
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多