ConstraintLayout - 将孩子的大小调整为其他孩子的某个百分比,而不是父母

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

假设我在 ConstraintLayout 中有两个子视图,并且想将第二个视图层叠在第一个视图之上。
此外,第二个的大小应该是另一个的某个百分比,例如 40% 宽度和 60% 高度。
我还希望它在第一个视图中有一些相对偏移,比如 20% 的宽度和 30% 的高度。

下面是我希望这样做的代码。

<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" >

    <View
        android:id="@+id/firstView"
        android:layout_height="250dp"
        android:layout_width="250dp"
        android:background="@color/white" />

    <View
        android:id="@+id/secondView"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="@+id/firstView"
        app:layout_constraintEnd_toEndOf="@+id/firstView"
        app:layout_constraintStart_toStartOf="@+id/firstView"
        app:layout_constraintTop_toTopOf="@+id/firstView"
        app:layout_constraintWidth_percent="0.4"
        app:layout_constraintHeight_percent="0.6"
        app:layout_constraintHorizontal_bias="0.2"
        app:layout_constraintVertical_bias="0.3" />

</androidx.constraintlayout.widget.ConstraintLayout>

偏移(偏差)部分按预期工作 - 第二个视图相对于第一个视图偏移了一定百分比。
然而,secondView 的大小是相对于父 ContraintLayout 的大小,而不是相对于 firstView。
documentation 确认并说:

MATCH_CONSTRAINT_PERCENT -- 维度将是另一个小部件的百分比(默认情况下,父)

在我的例子中,我想说“另一个小部件”是 firstView,而不是父视图。 我在 firstView 和 secondView 上尝试了很多不同的设置 - 但无济于事。 文档,甚至 ConstraintLayout 的源代码(据我所知)也没有帮助我。

如何实现这种相对大小?

android android-layout android-constraintlayout
1个回答
0
投票

我的解决方案是从 firstView 添加另一个具有相同重量和高度的布局,并将 secondView 放入其中。 这可能不是你想要的答案,但我相信它有效。

<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" >

 <View
        android:id="@+id/firstView"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:background="@color/red"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/firstView"
        app:layout_constraintEnd_toEndOf="@+id/firstView"
        app:layout_constraintStart_toStartOf="@+id/firstView"
        app:layout_constraintTop_toTopOf="@+id/firstView">

        <View
            android:id="@+id/secondView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.6"
            app:layout_constraintHorizontal_bias="0.2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.3"
            app:layout_constraintWidth_percent="0.4" />

</androidx.constraintlayout.widget.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.