我有 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>
黑色虚线是屏障。
这可能是一个错误或者我做错了,预览和实际设备中的结果相同
最佳答案
如果您指定
app:layout_optimizationLevel="none"
在 ConstraintLayout 的 XML 中,您会发现屏障将正确放置。我不确定设置优化级别会达到什么效果,但最近它一直是障碍的问题。 (约束布局版本 2.1.3)。这是在抑制优化之前布局的外观。如前所述,障碍上升到正确的 TextView 中。
我们通过在 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">
现在布局看起来像这样:障碍已下降到它所属的右侧 TextView 下方。
这是 ConstraintLayout 2.1.3 版。
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
(似乎将优化级别设置为 standard
以外的任何值都可以解决此问题。)
关于android - 约束布局屏障未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71216910/