【问题标题】:ConstraintLayout hides the last line of TextView with app:layout_constrainedHeight="true"ConstraintLayout 用 app:layout_constrainedHeight="true" 隐藏 TextView 的最后一行
【发布时间】:2020-05-15 16:24:50
【问题描述】:

我注意到ConstraintLayout(1.1.3 版)的奇怪行为,每当我尝试使用带有wrap_content 属性的高度并且layout_constrainedHeight 设置为true 时隐藏TextView 的最后一行。

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>

我认为每当我想将wrap_contentConstraintLayout 一起使用时,我必须将layout_constrainedHeight 设置为true,但这有时会给我带来奇怪的错误。我错过了什么吗?

编辑

如果我删除TextView 周围的边距,它可以正常工作。似乎ConstraintLayoutwrap_content 和边距有问题。

【问题讨论】:

  • 我可以确认这一点。它发生在我身上,解决方案包括重新考虑布局。你应该提交issue at google's tracker for ConstraintLayout
  • 将您的布局升级到implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta6"

标签: android android-constraintlayout android-jetpack


【解决方案1】:

根据documentation

WRAP_CONTENT : 强制约束(在 1.1 中添加)

如果将维度设置为 WRAP_CONTENT,则在 1.1 之前的版本中,它们将被视为文字维度——也就是说,约束不会限制生成的维度。 虽然通常这已经足够(而且速度更快),但在某些情况下,您可能希望使用 WRAP_CONTENT,但继续强制执行约束以限制结果维度。在这种情况下,您可以添加相应的属性之一:

app:layout_constrainedWidth=”true|false”

app:layout_constrainedHeight=”true|false”

我将强调这句话的一部分: 继续执行约束以限制结果维度

所以基本上,将layout_constrainedHeightlayout_constrainedWidth 设置为true 将保留约束并将视图大小减小指定的边距,而不是推动所有其他视图并增加当前视图的高度/宽度以适应内容。

这是一个带有app:layout_constrainedHeight="true"app:layout_constrainedWidth=”true” 以及不同边距的示例。红色TextView 包裹了它的内容,然后缩小了大小。绿色 TextView 没有设置 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 代码并记录了a little video 以查看差异。

【讨论】:

  • 感谢您的回答。我理解我写的内容,但问题是此示例中没有高度限制。 TextView 可以毫无障碍地伸展它的高度。我更新了图片。
  • @Nominalista,我已经更新了答案。看起来有高度限制,它等于(最小视图高度 w/o 边距 + 填充)。然后从该值中减去边距值,为您提供在内容末尾剪切的视图。
【解决方案2】:

尝试为 TextView - app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintBottom_toTopOf="@id/someOtherView" 的底部添加一个约束,并且此 someOtherView 应该对父级具有底部约束。应该有一个垂直链,app:layout_constrainedHeight 才能工作。

希望对你有帮助。

【讨论】:

  • 不幸的是,将约束添加到父级底部并不能解决此问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
相关资源
最近更新 更多