Lec*_*ius 7 xml layout android android-layout
我试图查看app:layout_constrainedWidth="true"在 ConstraintLayout 中使用 的示例,但我并不理解此属性的使用。在什么情况下应该使用此属性设置为 true?谢谢您的帮助
Che*_*amp 13
app:layout_constrainedWidth="true" 使ConstraintLayout遵守宽度为“wrap_content”的视图的约束。这是更完整的解释。
WRAP_CONTENT :强制约束(1.1 中添加)
如果维度设置为 WRAP_CONTENT,则在 1.1 之前的版本中,它们将被视为文字维度 - 这意味着约束不会限制结果维度。虽然一般来说这已经足够了(而且更快),但在某些情况下,您可能想要使用 WRAP_CONTENT,但仍要继续强制执行约束来限制结果维度。在这种情况下,您可以添加相应的属性之一:
Run Code Online (Sandbox Code Playgroud)app:layout_constrainedWidth="true|false" app:layout_constrainedHeight="true|false"
假设我们有以下布局:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Left TextView"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.8" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="I am a TextVIew with some very long text. How is it going to be handled?"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这显示为
正如您所看到的,正确的TextView文本被截断 - 不遵守正确的约束。现在让我们添加app:layout_constrainedWidth="true"到右侧的TextView。现在将会看到
视图仍然是wrap_content,但尊重正确的约束。
| 归档时间: |
|
| 查看次数: |
4170 次 |
| 最近记录: |