android - 约束布局屏障未按预期工作

标签 android xml android-layout android-constraintlayout constraintlayout-barrier

我有 2 个 View ,我需要在下面设置一个屏障,但该屏障无法按预期工作。
这是我的布局。

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

    <TextView
        android:id="@+id/textView15"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="This is a text view"
        app:layout_constraintEnd_toStartOf="@+id/t1"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/t1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView15"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a demo text to check wrap content"/>

    </com.google.android.material.textfield.TextInputLayout>

    <androidx.constraintlayout.widget.Barrier
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView15,t1"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Constraint layout preview
黑色虚线是屏障。
这可能是一个错误或者我做错了,预览和实际设备中的结果相同

最佳答案

如果您指定

app:layout_optimizationLevel="none"
在 ConstraintLayout 的 XML 中,您会发现屏障将正确放置。我不确定设置优化级别会达到什么效果,但最近它一直是障碍的问题。 (约束布局版本 2.1.3)。
这是在抑制优化之前布局的外观。如前所述,障碍上升到正确的 TextView 中。
enter image description here
我们通过在 XML 中声明而不进行其他更改来抑制优化:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_optimizationLevel="none"
    xmlns:app="http://schemas.android.com/apk/res-auto">
现在布局看起来像这样:
enter image description here
障碍已下降到它所属的右侧 TextView 下方。
这是 ConstraintLayout 2.1.3 版。
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
(似乎将优化级别设置为 standard 以外的任何值都可以解决此问题。)

关于android - 约束布局屏障未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71216910/

相关文章:

python - 解析 XML lxml 我应该跳过或删除注释阻止解析吗

android - 哪种 Android 布局和 View 用于简单的基于图像的游戏?

android - 创建 QR 码以在 iPhone 和 Android 上发送电子邮件

java - 在 Android 中无法同时关闭 2 个 AlertDialog

java - 如何解决这个错误: Android resource linking failed?

c# - 如何将 XML 文件反序列化为具有只读属性的类?

java - Stax问题解析文档,其结束元素和开始元素在同一行

java - 系统默认下方的 Android 自定义应用栏

android - 类似 iPhone 的滑动表头,或 : how to force immediate redraw on top margin change?

android - 将大量数据下载到 android 的最有效方法?