如何使用 ConstraintLayout 让第一个 View 缩小以适应剩余空间?

2024-04-07

我有以下布局。注意Z位于下方Y,但限制在底部。之间有一个很好的间隙Y and Z,由多余的垂直空间提供。这是我的desired and actual当存在时的行为多余的垂直空间.

但是,当显示键盘时,我用完了多余的垂直空间。

期望的行为(没有多余的垂直空间)当我用完垂直空间时,我希望发生以下情况:X (ScrollView),收缩以填充剩余空间,允许Y and Z以全尺寸显示。

实际行为(没有多余的垂直空间) Y反而缩小了。

我的来源如下。我如何修改它才能在两种情况下获得我想要的行为(多余的垂直空间和没有多余的垂直空间)?

<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">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fbe9e7"
            android:gravity="center"
            android:text="X"
            android:textSize="96sp">

        </TextView>
    </ScrollView>

    <TextView
        android:id="@+id/text_Y"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f3e5f5"
        android:gravity="center"
        android:text="Y"
        android:textSize="96sp"
        app:layout_constraintTop_toBottomOf="@+id/scrollView" />

    <TextView
        android:id="@+id/text_Z"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e1f5fe"
        android:gravity="center"
        android:text="Z"
        android:textSize="96sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_Y"
        app:layout_constraintVertical_bias="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

这个问题很大程度上源于X滚动视图需要0dp当垂直空间有限时,但是wrap_content当有多余的垂直空间时

Note: you can demo how the layout will behave with less vertical space by dragging the bottom right corner accordingly in the preview pane for a layout in Android Studioenter image description here


try this

<?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">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@+id/text_Y"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintVertical_chainStyle="packed">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fbe9e7"
            android:gravity="center"
            android:text="X"
            android:textSize="96sp">

        </TextView>
    </ScrollView>

    <TextView
        android:id="@+id/text_Y"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f3e5f5"
        android:gravity="center"
        android:text="Y"
        android:textSize="96sp"
        app:layout_constraintBottom_toTopOf="@+id/text_Z"
        app:layout_constraintTop_toBottomOf="@+id/scrollView"
        app:layout_constraintVertical_bias="0" />

    <TextView
        android:id="@+id/text_Z"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e1f5fe"
        android:gravity="center"
        android:text="Z"
        android:textSize="96sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

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

如何使用 ConstraintLayout 让第一个 View 缩小以适应剩余空间? 的相关文章

随机推荐

  • 如何在 jQuery 中设置文本框值?

    如何使用 jQuery 将某个值正确加载到文本框中 尝试了下面的 但我得到了 object Object 作为输出 请告诉我这一点 我是 jQuery 的新手 proc function x y var str1 pid value var
  • 使用整数作为模板参数时出现编译错误

    下面这段代码有什么问题 template
  • lxml 更改 Unicode 字符

    我正在使用 lxml 读取 xml 文件并更改一些详细信息 但是 运行时我发现即使我只是使用lxml读取文件然后再次写出来 如下所示 fil iTunes Music Library XML tre etree parse fil tre
  • Bootstrap 图标未显示在已发布的 ASP.NET MVC 应用程序中

    注意 请前往编辑 2 部分查看摘要 我有一个 ASP NET MVC 4 应用程序 我将 twitter Bootstrap 集成到其中 Bootstrap 工作正常 但当我发布应用程序时 图标无法正确显示 我尝试重新发布该应用程序 但没有
  • 在 XAML 中设置命令目标

    我很难理解 RoutedCommand 的 CommandTarget 属性 基本上 我有一些在用户控件 而不是窗口 中实现的静态命令 我在用户控件中创建命令绑定 如果我在用户控件中声明按钮 那么我就可以使用我的路由事件 但是 当按钮位于用
  • C++0x 右值引用和临时值

    我在 comp std c 上问了这个问题的变体 但没有得到答案 为什么调用f arg 在此代码中调用 const ref 重载f void f const std string less efficient void f std stri
  • 我不应该调用 HostingEnvironment.UnregisterObject 吗?

    在尝试在我的 ASP Net MVC 3 应用程序中通过 smtp 实现异步电子邮件时 我遇到了问题SO SmtpClient SendAsync 阻止我的 ASP NET MVC 请求 https stackoverflow com qu
  • 使用 dplyr 连接组内的所有行

    假设我有一个像这样的数据框 hand id card id card name card class A 1 p alpha A 2 q beta A 3 r theta B 2 q beta B 3 r theta B 4 s gamma
  • 在msbuild中过滤项目的元数据

    我想使用修改后的元数据创建一个新的项目集合 例如 更改 ClCompile AdditionalIncludeDirectories 的分隔符 为此 我首先从AdditionalIncludeDirectories 元数据创建一个项目集合
  • 如何更改 Scalatra 应用程序的“webapp”目录位置?

    默认情况下 Scalatra 期望 webapp 目录位于src main webapp 如何将其更改为 例如content doc root sbt 允许使用如下内容自定义其默认目录 scalaSource lt lt baseDirec
  • 获取 iPhone 上当前的系统音量级别

    有什么方法可以获取 iPhone 上当前的系统音量级别吗 我在想也许有一种方法可以制作MPVolumeView并从中获取价值 musicPlayer MPMusicPlayerController iPodMusicPlayer curre
  • 我如何知道我正在使用哪个版本的 OpenGL?

    我开始使用 GLFW 和 OpenGL 用 C 目前 编写程序 我的问题是 我如何知道我的程序将使用哪个版本的 OpenGL 我的笔记本电脑显示我的显卡支持 OpenGL 3 3 输入 glxinfo grep i opengl 返回 Op
  • 从现有虚拟机创建 docker 镜像

    我需要使用 CentOS 和 MySQL 创建 docker 基础镜像 但我已经有这样的虚拟机 没有docker 如何从现有的虚拟机创建基础 docker 镜像并在另一台带有 docker 的机器上使用它 虽然其他评论者正确地指出 将虚拟机
  • 如何获取 pandas .p​​lot(kind='kde') 的输出

    当我绘制我的 pandas 系列的密度分布时 我使用 plot kind kde 是否可以获得该图的输出值 如果是的话该怎么做 我需要绘制的值 plot kind kde https pandas pydata org docs refer
  • 确定选择哪个 JRadioButton 的最佳方法是什么?

    目前我正在以这种方式获取选定的按钮 但如果这是正确 最好的方法 我不会 也许有比这更简单或更面向对象的东西 private int getFilterType JRadioButton buttons for int i 0 n butto
  • 使用 Angular 将日期转换为东部时区

    我从服务器获取数据如下 2015 03 05T16 51 56 00 00 使用 Angular 我想将此日期 时间显示为东部时间日期 有没有办法用 Angular 指定不同的时区 我正在做类似的事情 myDate date medium
  • Android 持久内容观察者

    Android 中的内容观察者是否持久 如果我在活动中创建内容观察者 该观察者是否会继续运行 直到我删除该观察者 基本上 我正在创建一项短信服务 在接收和发送时 我将短信发布到网络服务 这样我就可以在没有手机的情况下检查我的消息 如果内容观
  • libAdIdAccess.a 位置?

    按照说明在 iOS 上将 IDFA 与 Google Analytics 结合使用 https developers google com analytics devguides collection ios v3 optional fea
  • Pandas groupby 多个字段然后 diff

    所以我的数据框看起来像这样 date site country score 0 2018 01 01 google us 100 1 2018 01 01 google ch 50 2 2018 01 02 google us 70 3 2
  • 如何使用 ConstraintLayout 让第一个 View 缩小以适应剩余空间?

    我有以下布局 注意Z位于下方Y 但限制在底部 之间有一个很好的间隙Y and Z 由多余的垂直空间提供 这是我的desired and actual当存在时的行为多余的垂直空间 但是 当显示键盘时 我用完了多余的垂直空间 期望的行为 没有多