Nom*_*sta 5 android android-constraintlayout android-jetpack
我注意到ConstraintLayout(版本 1.1.3)的奇怪行为,TextView每当我尝试将 height 与wrap_content属性一起使用并layout_constrainedHeight设置为true.
与layout_constrainedHeight:
没有layout_constrainedHeight:
源代码:
<?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:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Lorem..."
app:layout_constrainedHeight="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我认为每当我想使用wrap_contentwith 时,ConstraintLayout我都必须设置layout_constrainedHeight为 true,但这有时会给我带来奇怪的错误。我错过了什么吗?
编辑
如果我删除 周围的边距TextView,它工作正常。看来,ConstraintLayout做一些错误wrap_content和利润。
根据文件:
\n\n\n\n\nWRAP_CONTENT :强制约束(1.1 中添加)
\n\n如果维度设置为 WRAP_CONTENT,则在 1.1 之前的版本中,它们将被视为文字维度 - 这意味着约束不会限制结果维度。\n 虽然一般来说这已经足够了(而且更快),但在某些情况下,您可能想要使用 WRAP_CONTENT,但仍要继续强制执行约束来限制结果维度。在这种情况下,您可以添加相应的属性之一:
\n\n应用程序:layout_constrainedWidth = \ xe2 \ x80 \ x9dtrue | false \ xe2 \ x80 \ x9d
\n\n应用程序:layout_constrainedHeight = \ xe2 \ x80 \ x9dtrue | false \ xe2 \ x80 \ x9d
\n
我将强调这句话的一部分:\n继续实施约束以限制结果尺寸
\n\n所以基本上,设置layout_constrainedHeight或layout_constrainedWidthtotrue将保留约束并按指定的边距减小视图大小,而不是推动所有其他视图并增加当前视图高度/宽度以适应内容。
app:layout_constrainedHeight="true"这是一个具有和和 不同边距的示例app:layout_constrainedWidth=\xe2\x80\x9dtrue\xe2\x80\x9d。红色TextView包裹了它的内容,然后缩小了尺寸。绿色TextView没有app:layout_constrained...="true"设置属性和边距。它们的高度相等,但宽度最终不同。
布局:
\n\n<?xml version="1.0" encoding="utf-8"?>\n<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n\n <TextView\n android:id="@+id/top_text_view"\n android:layout_width="0dp"\n android:layout_height="wrap_content"\n android:layout_marginStart="86dp"\n android:layout_marginEnd="26dp"\n android:background="#ff2356"\n android:text="@string/lorem_kind_of"\n android:textColor="@android:color/white"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent"\n\n\n app:layout_constrainedHeight="true"/> <!-- This line is the only difference -->\n\n <TextView\n android:id="@+id/bottom_text_view"\n android:layout_width="0dp"\n android:layout_height="wrap_content"\n android:layout_marginStart="86dp"\n android:layout_marginEnd="26dp"\n android:background="#009900"\n android:text="@string/lorem_kind_of"\n android:textColor="@android:color/white"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toBottomOf="@+id/top_text_view" />\n\n</androidx.constraintlayout.widget.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\n\n我的猜测是您可能不需要使用app:layout_constrainedHeight属性。欢迎您发表评论,如果我的回答不能解决您的问题,我们将进一步详细说明。
看起来您想要的行为只有在没有app:layout_constrainedHeight="true". 我可能是错的,这取决于最终想要的结果,但根据我的“实验”,看起来像是app:layout_constrainedHeight限制了进一步增长其最小尺寸的观点。
我更新了 XML 代码并录制了一些视频来查看差异。
\n| 归档时间: |
|
| 查看次数: |
928 次 |
| 最近记录: |