android - <ConstraintLayout/> 中 <TextView/> 下方的边距不起作用

标签 android xml android-recyclerview android-constraintlayout

ConstraintLayout 中有一个简单的布局,包含 TextViewRecyclerView

    <android.support.constraint.ConstraintLayout
        android:id="@+id/clSelectedPatient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvSelectPatient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lifcare"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginBottom="100dp"
            app:layout_constraintBottom_toTopOf="@+id/rvPatients"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPatients"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:layout_constraintTop_toBottomOf="@+id/tvSelectPatient"/>

    </android.support.constraint.ConstraintLayout>

android:layout_marginBottom="100dp" TextView 不工作。

最佳答案

您的布局中存在一些错误:

  • 您对宽度使用 match_parent,并且禁止对 ConstraintLayout subview 使用 match_parent。

Build a Responsive UI with ConstraintLayout - Adjust the view size

Note: You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp)

  • 为了正确显示垂直边距,您必须为 TextView 和 RecyclerView 定义垂直约束。

您的固定布局代码如下所示:

<android.support.constraint.ConstraintLayout
    android:id="@+id/clSelectedPatient"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvSelectPatient"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
        android:text="Lifcare"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/rvPatients" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvPatients"
        android:layout_width="0dp"
        android:layout_height="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvSelectPatient"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

这是它在设备上的样子:

enter image description here

关于android - <ConstraintLayout/> 中 <TextView/> 下方的边距不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47287966/

相关文章:

android - 错误地删除了 fragment_my.xml?如何重新生成它?

java - 错误:程序类型已存在:com.google.firebase.iid.FirebaseInstanceId

java - 从 XML 到 Java 的字符编码

android - 清除整个适配器并添加新元素时的 RecyclerView 动画

android - 当卡不在视野中时,FindViewHolderForPosition 返回 null

android - 如何保护App中的Xml文件?

android - ClearableEditText - requestLayout() 在 Android 4.3 上调用不当

java - JSON 到 Xml - 带连字符的元素名称转换

html - 在 XSL 中双重转义原始 HTML?

java - 更新 RecycleView 中的 CardView 项目时遇到延迟