ConstraintLayout 中的边距如何工作?
How do margins work in a ConstraintLayout?
main_layout.xml:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CollapsingTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
它看起来像边界 ConstraintLayout 不考虑 any 水平边距,只是水平居中 TextView
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CollapsingTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
那是因为您将 TextView 的宽度设置为 "wrap_content"
。
要强制它适应您需要使用宽度 "0dp"
的约束。这是 "match_constraint"
的功能等价物,但目前未被识别为一个选项。
main_layout.xml:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CollapsingTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
它看起来像边界 ConstraintLayout 不考虑 any 水平边距,只是水平居中 TextView
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CollapsingTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
那是因为您将 TextView 的宽度设置为 "wrap_content"
。
要强制它适应您需要使用宽度 "0dp"
的约束。这是 "match_constraint"
的功能等价物,但目前未被识别为一个选项。