0

我有一个ScrollView其中有一个ConstraintLayout.

在里面ConstraintLayout我想将 a 定位View到 a 的底部Barrier

Barrier本身有约束: a Guideline(它也可以是高度为父级一半的视图),它是ConstraintLayout的一半和 a TextView

这里的问题是ConstraintLayout由于屏幕百分比计算的一半,它将扩大更多(添加更多可滚动区域)。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:background="#39537A4B"
        android:fillViewport="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:id="@+id/topHalf"
                android:layout_width="10dp"
                android:layout_height="0dp"
                android:background="#99B146B8"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.5"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0" />

            <View
                android:id="@+id/bottomHalf"
                android:layout_width="10dp"
                android:layout_height="0dp"
                android:background="#99B89F46"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.5"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="1.0" />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Do smth \n\n\n\n\n\n\nasdasda"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.5" />

            <androidx.constraintlayout.widget.Barrier
                android:id="@+id/barrier"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:barrierDirection="bottom"
                app:constraint_referenced_ids="guideline, text" />

            <View
                android:id="@+id/view"
                android:layout_width="60dp"
                android:layout_height="40dp"
                android:background="#651F7878"
                app:layout_constraintStart_toStartOf="@id/barrier"
                app:layout_constraintTop_toBottomOf="@id/barrier" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

尝试将Barrier约束更改为:

app:constraint_referenced_ids="text"

你明白这会Barrier阻碍计算。

我知道如何从 java 解决这个布局问题,我想要一个来自 xml 的解决方案。这可能是一个错误ConstraintLayout

4

1 回答 1

0

这种行为很奇怪,肯定与屏障和wrap_content ConstraintLayout相关。这是展示该行为的简化布局。

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_light"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <View
        android:id="@+id/redBox"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".5" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="redBox,guideline"/>

</androidx.constraintlayout.widget.ConstraintLayout>

使用上面的布局,以下是显示的内容。虽然ConstraintLayoutwrap_content,但它扩展为红框高度的两倍。

在此处输入图像描述

如果我们从屏障中移除红色框或指南,则布局行为:

在此处输入图像描述

ConstraintLayout的高度可以通过改变准线的位置来改变。这里是设置为 30% 的指导方针:

在此处输入图像描述

IMO 的预期行为是ConstraintLayout将根据其子视图的高度调整自身大小,这里只是红色框,并且将相应地设置指南。这显然不是正在发生的事情。

也许有人可以找出使用简化布局的 XML 解决方法。我没有从粗略的搜索中看到类似的问题,因此可能值得报告。

(使用ConstraintLayout 2.1.0 版)

于 2021-09-10T12:02:52.737 回答