ConstraintLayout 隐藏 TextView 的最后一行 app:layout_constrainedHeight="true"

2023-12-29

我注意到奇怪的行为ConstraintLayout(版本 1.1.3)隐藏最后一行TextView每当我尝试使用高度时wrap_content财产和layout_constrainedHeight被设定为true.

With layout_constrainedHeight:

With no 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_content with ConstraintLayout我必须设置layout_constrainedHeight是的,但这有时会给我带来奇怪的错误。我错过了什么吗?

EDIT

如果我去掉周围的边距TextView,效果很好。看起来ConstraintLayout做错了什么wrap_content和边距。


根据文档 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看看差异。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ConstraintLayout 隐藏 TextView 的最后一行 app:layout_constrainedHeight="true" 的相关文章

随机推荐

  • 如何强制执行软件试用安装的到期日期?

    我需要为我的定制软件提供试用期 我有一个 64 位 C 应用程序 它使用 64 位多处理器支持 DLL 其他要包含的实用程序是用 C 编写的 exe 和 dll 我必须能够加密 C 代码 将软件加密为仅在特定计算机上运行 并强制执行不能通过
  • 使用 Excel 宏/vba 打开/关闭图表系列的可见性

    我正在 Excel 中制作折线图 图表 并将多个数据系列绘制到同一个图表上 我需要创建一个宏 VBA 解决方案 可以通过按下按钮 或复选框等 打开 关闭这些系列的可见性 类似这张图 通过excel菜单系统手动完成 我试图查看所有成员变量 方
  • 在位图图像中设置背景颜色

    我想将画布保存为图像 它可以工作 但背景颜色是黑色 我必须如何添加才能改变颜色 我使用这段代码 Size size new Size surface Width surface Height surface Measure size sur
  • 如何打印带有参数值的调用堆栈?

    功能traceback print stack 打印调用堆栈 如果我们可以看到每个级别的参数值 将有助于调试 但我找不到办法做到这一点 例如 def f1 a 2 f2 a a 1 def f2 a 3 f3 def f3 a 4 prin
  • 为什么 MongoDB 文档建议不要使用 DBREF?

    The DBREF 的 MongoDB 文档 https docs mongodb org manual reference database references say 除非您有令人信服的理由使用 DBRef 否则请使用手动引用 为什么
  • 如何减少对模板函数的调用

    有一个功能 template
  • 有没有可以填充本机 Windows 表单字段的 python 模块?

    我想知道有哪些 Python 模块可以帮助我在本机 Windows 应用程序 IDE 中输入表单数据 例如 受密码保护的 USB 具有输入密码的应用程序 我想通过运行应用程序并使用密码填写表单来自动化此过程 提前致谢 roo7break 图
  • 如何在 Visual Studio 中的匿名命名空间内按函数名称设置断点?

    我有以下代码 namespace void Foo namespace Bar void Foo int main Foo Bar Foo return 0 我想设置断点Foo 按名称在匿名命名空间内 Ctrl B 键绑定 我可以对命名空间
  • iOS 应用程序启动时间测量

    如何计算从用户按下主屏幕上的启动按钮到应用程序处于活动状态 例如 直到第一个视图控制器的 viewDidAppear 方法 所需的时间 Id 不需要以编程方式实现 但必须可靠 类似地 在 Android 中 logcat 可以用于此效果 Y
  • 在 JSF 中分配“值表达式”代替“方法表达式”

    在我的复合组件中 我迭代了list
  • 在导航栏中按下后退按钮时会发生什么

    我想知道按下导航栏上的后退按钮时调用的函数是什么 我想在按下按钮时添加一些功能 谁知道呢 提前致谢 您想要的功能位于UINavigationBarDelegate协议 实施 navigationBar shouldPopItem 方法并将你
  • 为什么“htons”和“ntohs”同时存在?

    我不知道为什么htons and ntohs两者都存在于标准库中 他们做的事情完全相同 除非我不知何故感到困惑 同样适用于htonl and ntohl 它们制作自记录代码 告诉读者数据是按主机顺序还是网络顺序
  • 将 C# 中的结构序列化为 C++,反之亦然

    有没有一种简单的方法可以序列化 C 结构 然后从 C 反序列化它 我知道我们可以将 csharp 结构序列化为 xml 数据 但我必须在 c 中实现 xml 反序列化器 C 中哪种序列化器最容易从 C 反序列化 我希望两个应用程序 一个 C
  • 使用哪些 NuGet 源进行恢复?

    我正在做一个简单的dotnet restore在我的Mac上 我正在使用 目前 最新的dotnet CLI 版本 2 2 https dotnet microsoft com download 由于您的项目文件夹中可以同时拥有 NuGet
  • Caddy - 如何仅为一个域禁用 https

    我有一个 Caddy v2 的配置文件 如下所示 sentry mydomain ru reverse proxy sentry 9000 tasks mydomain ru reverse proxy taiga proxy 80 ain
  • 网格数据结构

    通常 可扩展 网格表示为列表列表 行列表 每行都有单元格列表 这些列表是某种链接列表 在此数据结构中操作 删除 插入 行既简单又便宜 只需重新链接以前的节点即可 但是当涉及到列时 例如删除列 它会变成一个非常长的操作 我需要 循环 要删除索
  • array+=value 在 ksh 中不起作用?

    我在某处读到 ksh 的数组支持 附加新元素 但我尝试了它 但它不起作用 ksh arr a b c d ksh arr e ksh echo arr ae b c d ksh 为什么arr 0 变成ae 要将一个元素添加到数组中 应该如下
  • 如何在不阻塞的情况下确定 goroutine 是否已完成?

    到目前为止我见过的所有例子都涉及阻塞以获得结果 通过 lt chan操作员 我当前的方法涉及传递指向结构的指针 type goresult struct result resultType finished bool goroutine 在
  • 在后台选项卡上运行画布

    我最近创建了一个 HTML5 画布动画 也使用Processing js 问题是 当我将浏览器切换到不同的选项卡时 动画停止播放 当用户位于与包含动画的选项卡不同的选项卡时 如何允许动画继续播放 例子 http jsfiddle net E
  • ConstraintLayout 隐藏 TextView 的最后一行 app:layout_constrainedHeight="true"

    我注意到奇怪的行为ConstraintLayout 版本 1 1 3 隐藏最后一行TextView每当我尝试使用高度时wrap content财产和layout constrainedHeight被设定为true With layout c