ConstraintLayout
中有一个简单的布局,包含 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="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>
这是它在设备上的样子:
关于android - <ConstraintLayout/> 中 <TextView/> 下方的边距不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47287966/