ConstraintLayout:从wrap_content高度计算中排除子项

问题描述 投票:-1回答:1

考虑这个简单的ConstraintLayout layout.xml

<?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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f00">

    <View
            android:id="@+id/partOfWrapContent"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:background="#0f0" />

    <View
            android:id="@+id/shouldBeExcludedFromWrapContent"
            android:layout_width="10dp"
            android:layout_height="100dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:background="#00f" />

</androidx.constraintlayout.widget.ConstraintLayout>

这将导致以下布局(androidx.constraintlayout:constraintlayout:1.1.32.0.0-beta4都使用:]

actual result

问题

如何使wrap_content高度计算忽略@id/shouldBeExcludedFromWrapContent?我希望剪切它,所以最终结果是这样的:

enter image description here

注意:

  • [android:layout_height必须为“ wrap_content”,因为实际代码将位于ScrollView]中>
  • @id/shouldBeExcludedFromWrapContent放入match_parent内的ConstraintLayout容器中可起到剪切效果,但这不是一个选择,因为在实际代码中@id/shouldBeExcludedFromWrapContent必须相对于其同级进行布局。
  • 在真实代码中,@id/shouldBeExcludedFromWrapContent的动画制作方式是使其整个高度逐渐向用户显示,因此不能通过简单地将其高度设置为40dp来“伪造”剪辑。

考虑此简单的ConstraintLayout layout.xml:

不确定,但这可能是您正在寻找的解决方案。除partOfWrapContent视图外,使约束布局高度的所有子项都为match_parent。因此,约束布局将获得partOfWrapContent视图的高度。这可能会解决您的问题。希望对您有所帮助。

更新:尝试将最大高度设置为如下所示的约束布局:

android:layout_height="wrap_content"
app:layout_constraintHeight_max="40dp"
app:layout_constrainedHeight="true"

android android-constraintlayout
1个回答
0
投票

不确定,但这可能是您正在寻找的解决方案。除partOfWrapContent视图外,使约束布局高度的所有子项都为match_parent。因此,约束布局将获得partOfWrapContent视图的高度。这可能会解决您的问题。希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.