根据文档 https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#wrap_content-:-enforcing-constraints-added-in-1.1:
WRAP_CONTENT :强制约束(1.1 中添加)
如果维度设置为 WRAP_CONTENT,则在 1.1 之前的版本中,它们将被视为文字维度 - 这意味着约束不会限制结果维度。
虽然一般来说这已经足够了(而且更快),但在某些情况下,您可能想要使用 WRAP_CONTENT,但仍要继续强制执行约束来限制结果维度。在这种情况下,您可以添加相应的属性之一:
应用程序:layout_constrainedWidth =“真|假”
应用程序:layout_constrainedHeight =“true | false”
我要强调一下这句话的一段话:继续实施约束以限制结果尺寸
所以基本上,设置layout_constrainedHeight
or layout_constrainedWidth
to true
将保留约束并按指定的边距减小视图大小,而不是推动所有其他视图并增加当前视图的高度/宽度以适应内容。
这是一个例子app:layout_constrainedHeight="true"
and app:layout_constrainedWidth=”true”
和不同的边距。红色的TextView
包裹它的内容,然后缩小大小。绿色的TextView
has no app:layout_constrained...="true"
属性和边距设置。它们的高度相等,但宽度最终不同。
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/top_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="86dp"
android:layout_marginEnd="26dp"
android:background="#ff2356"
android:text="@string/lorem_kind_of"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true"/> <!-- This line is the only difference -->
<TextView
android:id="@+id/bottom_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="86dp"
android:layout_marginEnd="26dp"
android:background="#009900"
android:text="@string/lorem_kind_of"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的猜测是你可能不需要使用app:layout_constrainedHeight
属性。欢迎您发表评论,如果我的回答不能解决您的问题,我们将进一步详细说明。
更新(2020 年 5 月 22 日)
看起来您想要的行为只有在没有app:layout_constrainedHeight="true"
。我可能是错的,这取决于最终想要的结果,但根据我的“实验”看起来像app:layout_constrainedHeight
限制了其进一步增长的最小尺寸。
我已经更新了 XML 代码并记录了一个小视频 https://youtu.be/nLotEkiKDEQ看看差异。