如何在约束布局中将一个视图与另一个视图的中心对齐

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

在上图中,我必须将这些点(它们只是 XML 中的空视图)与其上方图标的中心对齐

下面的代码是我的回收器视图,其中包含视图的

<androidx.core.widget.NestedScrollView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/itemImage">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nestedRecyclerView"/>

</androidx.core.widget.NestedScrollView>

上面的代码是图标的

 <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/itemImage"
        android:src="@drawable/ic_glucose"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

现在我需要将“NestedScrollView”与“ImageView”的中心对齐,以便点可以与图标的中心对齐。

如果我设置

app:layout_constraintLeft_toLeftOf="nestedScrollView"

它将完全向左对齐,但我希望它与图标的中心对齐

我该怎么做,请提出建议

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

不可能直接将 NestedScrollView 的左侧对齐到 ImageView 的中心,但有一种方法。

您可以将空视图与 ImageView 的中心对齐(ImageView 的从右到右,从左到左)。然后,您的 NestedScrollView 可以简单地与这个居中的 View 对齐,并且通过将 RecyclerView 与 NestedScrollView 的左侧对齐,您将获得所需的结果。


0
投票

只需将约束指定为要在中心对齐另一个视图的同一视图的顶部到顶部和底部到底部。

                 <TextView
                    android:id="@+id/tvPrintKOT"
                    **app:layout_constraintTop_toTopOf="@+id/rgKOT"
                    app:layout_constraintBottom_toBottomOf="@+id/rgKOT"**
                    app:layout_constraintStart_toStartOf="parent"
                    />

                <RadioGroup
                    **android:id="@+id/rgKOT"**
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toEndOf="@+id/tvPrintKOT"
                    app:layout_constraintTop_toBottomOf="@+id/tvTitle"
                    android:orientation="horizontal"
                    >
                </RadioGroup>
© www.soinside.com 2019 - 2024. All rights reserved.