带屏障的 ConstraintLayout;如何根据大小限制障碍物的底部/顶部

2023-12-02

我正在尝试 1.1.0-beta1 中的新 Barrier 功能。它按预期工作,但是有一个我似乎无法计算的用例或(或者根本可能有 ConstraintLayout 吗?)

我想要完成的是:我在左侧有一个图像视图,在图像视图的右侧有标题和副标题文本视图。 Imageview 是固定高度的。我有一个按钮,其底部限制在图像视图的底部however如果 subtitletextview 比 imageview 高,则顶部应限制在 subtitle textview 的底部。

(顺便说一句,我还尝试将其底部约束到图像视图的底部,将顶部约束到文本视图的底部,并给它1.0的底部偏差,但不适用于大文本情况)

对于障碍物,我只能将按钮顶部限制在障碍物底部(或顶部,似乎相同)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@color/blue"/>

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/books"/>

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="124dp"
        android:layout_height="156dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4"
        app:srcCompat="@android:drawable/radiobutton_on_background"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toRightOf="@+id/imageView6"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4"/>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Lorem Ipsum je fiktívny text, používaný pri návrhu tlačovín a typografie. Lorem Ipsum je štandardným výplňovým textom už od 16. storočia, keď neznámy tlačiar zobral sadzobnicu plnú tlačových znakov a pomiešal ich, aby tak vytvoril vzorkovú knihu. Prežil nielen päť storočí, ale aj skok do elektronickej sadzby, a pritom zostal v podstate nezmenený. Spopularizovaný bol v 60-tych rokoch 20.storočia, vydaním hárkov Letraset, ktoré obsahovali pasáže Lorem Ipsum, a neskôr aj publikačným softvérom"
        app:layout_constraintLeft_toLeftOf="@+id/textView4"
        app:layout_constraintRight_toRightOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView4"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintTop_toBottomOf="@+id/textView5"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

Both states in images: enter image description here


这是适合您的布局。它有点复杂,但可以满足您的要求。以下是它的工作原理和设置方式:

  • 定义一个不可见的按钮,blockingButton,其尺寸与其他按钮相同。将此按钮限制在左侧底部ImageView.

  • 在之上blockingButton,放置一个Space小部件。这个的位置Space小部件将定义底部按钮浮动的最大高度。

  • 定义一个漂浮在下方的障碍物Space小部件和大TextView.

  • 最后,将浮动按钮的顶部限制在障碍物上。

随着文字大小的变化很大TextView,按钮将向上浮动至Space小部件但仅此而已。作为TextView长大后,它会推下障碍物和底部按钮。

这里有一个短视频.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@color/blue" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/books" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="124dp"
        android:layout_height="156dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4"
        app:srcCompat="@android:drawable/radiobutton_on_background"
        android:layout_marginStart="8dp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toRightOf="@+id/imageView6"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:text="Lorem Ipsum je fiktívny text, používaný pri návrhu tlačovín a typografie. Lorem Ipsum je štandardným výplňovým textom už od 16. storočia, keď neznámy tlačiar zobral sadzobnicu plnú tlačových znakov a pomiešal ich, aby tak vytvoril vzorkovú knihu. Prežil nielen päť storočí, ale aj skok do elektronickej sadzby, a pritom zostal v podstate nezmenený. Spopularizovaný bol v 60-tych rokoch 20.storočia, vydaním hárkov Letraset, ktoré obsahovali pasáže Lorem Ipsum, a neskôr aj publikačným softvérom"
        app:layout_constraintLeft_toLeftOf="@+id/textView4"
        app:layout_constraintRight_toRightOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView4"
        app:layout_constraintHorizontal_bias="0.0" />

<!-- This is the start of the changes. -->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barrier" />

    <Button
        android:id="@+id/blockingButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/imageView6"
        app:layout_constraintLeft_toLeftOf="@+id/imageView6" />

    <android.support.v4.widget.Space
        android:id="@+id/spacer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/blockingButton" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="spacer, textView5" />

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

带屏障的 ConstraintLayout;如何根据大小限制障碍物的底部/顶部 的相关文章

  • 使用 xamarin 和 c# 更改 android 上的cultureinfo

    我调用自定义方法来动态地将当前文化信息切换为法语 fr 像这样 但在调用该方法后 我的 Android 应用程序仍然使用默认区域性 en 但在调试模式下 区域性似乎没问题 我的文件夹没问题 我两者都有 并且字符串值已配置 文件夹 resou
  • 可以挂载未加密的 obb 但出现加密错误 21

    这与 kitkat bug 无关 我正在 4 4 2 中测试 我可以毫无问题地挂载 obb 文件 问题是当尝试对加密的 obb 执行相同操作时 我在 Windows 中使用 jobb 如下 jobb d my folder o exp ob
  • 更新房间迁移的行

    是否可以编写迁移来更新某个表的所有先前数据 我正在为我的房间数据开发加密 如果我可以在迁移后加密所有行 那就太好了 那么 在定义迁移时 您可以访问支持SQLite数据库 https developer android com referen
  • 文件 API - Phonegap?

    谁能解释如何使用适用于 Android 的 Phonegap 文件 API 在页面上列出文件文件夹 我想列出所有 mp3如果可能的话 请阅读文件 但已阅读所有phonegap文档 http docs phonegap com en 1 0
  • 清除未接来电并清除 Android 栏中的通知

    使用此代码 我设法将所有未接来电标记为已读 ContentValues values new ContentValues values put Calls NEW 0 if android os Build VERSION SDK INT
  • Android Chrome 忽略 -webkit-text-size-adjust:none 属性。缩小时文本会缩放

    我们的客户请求一个网站 但不想支付移动版本的费用 我们仍在使其在移动设备上运行 当完全缩小时 Android 4 0 上的 Chrome 会缩放一堆文本 我们尝试设置 webkit text size adjust none 属性 但它似乎
  • 5.1 崩溃 - 任务描述的主要颜色应该是不透明的

    我已经在我的应用程序中实现了材料设计 它在 FATAL EXCEPTION main Process com test test PID 3195 java lang RuntimeException Unable to start act
  • Android 线程和处理程序不工作

    我最近重构了一个旧项目 发现无论我做什么 特定部分都不再需要正常运行 本质上 我有一个带有 TextView 的 Activity 该视图按时间间隔从同一类中调用的线程更新 暂停是通过 Thread sleep 完成的 并且使用 Handl
  • Android 设备与本地 API 服务器通信

    我想知道如何在外部设备上模拟我的 Android 应用程序 以便与我的计算机上运行的本地 API 服务器进行通信 该应用程序使用 cordova 并使用 USB 连接在 Android 设备上运行 我正在本地运行 API 服务器 使用我的机
  • Android API 列表

    我想获取 CellInfo 数据 但 cellinfo size 总是发生 模拟器中的 NullPointerException 细胞信息链接 http developer android com reference android tel
  • 从主线程查询android中的数据库

    我使用的是 sql lite 通常查询 1 个表 如果我从主 ui 线程进行查询会不会很糟糕 谢谢 这取决于 如果您的表确实很大 则执行查询可能需要一些时间 并且可能会导致您的应用程序出现明显的延迟 还有 你说你usually仅查询一张表
  • LibGDX 闪烁

    我已经使用 LibGDX UI 设置来启动一个项目 我在实现 ApplicationListener 中唯一拥有的是 public void create setScreen new LoadingScreen this 这应该会触发 Lo
  • Android MapView v2 黑屏

    我一直在尝试实现 android 的 MapView v2 除了这个错误之外 我让它工作得很好 This is what it looks like when I load the app from scratch 如您所见 没有任何问题
  • 更新到Android 5.0后出现java.lang.UnsatisfiedLinkError

    我正在开发一个 Android 应用程序 它使用一个名为 liballjoyn java so 的本机库 可用here https allseenalliance org developers download在 Android 核心 SD
  • ConstraintLayout 源代码位于哪里?

    哪里可以找到android的源代码ConstraintLayout 我在支持框架存储库甚至谷歌搜索中都找不到它 它的源代码在这里 https android googlesource com platform frameworks opt
  • 如何保存/加载 BigInteger 数组

    我想保存 加载BigInteger数组传入 传出 SharedPreferences 如何做呢 例如对于以下数组 private BigInteger dataCreatedTimes new BigInteger 20 Using Gso
  • 如何使用游戏循环每五秒在屏幕上出现和消失一个对象

    我正在尝试学习 Android 游戏开发 首先 我尝试每五秒使用游戏循环在屏幕上出现和消失一个对象 但我没有成功 我读过不同的教程和论坛 我按照教程中的方式应用了所有内容 但对象仍然在连续绘制 它并没有消失 我没有得到我所缺少的东西 请指导
  • Android 依赖项 com.google.firebase:firebase-core 编译 (12.0.1) 和运行时 (11.4.2) 的版本不同

    这是我的项目 Gradle 依赖项 classpath com android tools build gradle 3 1 0 classpath com google gms google services 3 0 0 classpat
  • 获取 Blob 图像并将该图像转换为 Bitmap 图像

    我正在从数据库中获取 blob 格式的图像 我想将其转换为位图图像 我用来将位图转换为 Blob 的代码放在下面 但是请告诉我如何反转它 ByteArrayOutputStream boas new ByteArrayOutputStrea
  • Android 中的自定义相机应用程序问题 - 旋转 270、拉伸捕获视图且未获取所有功能

    我从代码中得到了帮助https github com josnidhin Android Camera Example https github com josnidhin Android Camera Example 但面临一些问题 例如

随机推荐