使用ConstraintLayout将ChipGroup与ImageView对齐

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

我想使ImageGroup旁边的ChipGroup居中对齐。这是我的XML:

...

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

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/locationIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title"
            app:srcCompat="@drawable/ic_location_on_black_24dp" />

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/locationChips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            app:layout_constraintBottom_toBottomOf="@+id/locationIcon"
            app:layout_constraintStart_toEndOf="@id/locationIcon"
            app:layout_constraintTop_toTopOf="@id/locationIcon">

        </com.google.android.material.chip.ChipGroup>

    </androidx.constraintlayout.widget.ConstraintLayout>

...

Expected Result

Actual Result

如您所见,芯片没有对齐,而是位于标题文本的顶部,如果文本有一定长度,最后一个芯片也会溢出。我怎样才能解决这个问题?另外,我发现如果将ChipGroup限制为父代,则最后一个芯片不会溢出。

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

由于您基于locationIcon设置了约束,并且在locationIcon中设置了页边距上限,因此芯片未对齐。>

marginTop中删除ImageView

android:layout_marginTop="8dp"

也删除ChipGroup约束

app:layout_constraintBottom_toBottomOf="@+id/locationIcon"
© www.soinside.com 2019 - 2024. All rights reserved.