约束布局障碍未按预期工作

Ank*_*rma 6 xml android android-layout android-constraintlayout constraintlayout-barrier

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

<?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>
Run Code Online (Sandbox Code Playgroud)

约束布局预览

黑色虚线是屏障。

这可能是一个错误或者我做错了,预览和实际设备中的结果是相同的

Che*_*amp 13

如果您指定

app:layout_optimizationLevel="none"
Run Code Online (Sandbox Code Playgroud)

在ConstraintLayout的 XML 中,您会发现屏障已正确放置。我不确定设置优化级别能达到什么效果,但最近这是一个有障碍的问题。(ConstraintLayout版本 2.1.3)。

这是抑制优化之前的布局外观。如前所述,屏障上升到右侧的TextView中。

在此输入图像描述

我们通过在 XML 中声明而不进行其他更改来抑制优化:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_optimizationLevel="none"
    xmlns:app="http://schemas.android.com/apk/res-auto">
Run Code Online (Sandbox Code Playgroud)

现在布局如下所示:

在此输入图像描述

障碍物已降至其所属的右侧TextView下方。

这是ConstraintLayout版本 2.1.3 的情况。

implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
Run Code Online (Sandbox Code Playgroud)

(似乎将优化级别设置为任何值都standard不能解决此问题。)