Android 芯片组中的芯片如何居中对齐?

Android how to center align chips in chipgroup?

我在 XML 文件中为 ChipGroup 尝试了 gravity="center"foregroundGravity="center"textAlignment="center" 但它不起作用。有任何想法吗?

请尝试以下操作:

layout_gravity="center"

希望对您有所帮助。

把chip group的宽度填入wrap_content,让parent view to gravity到包含chip group的center。这样它就会居中对齐。

我尝试在 Flexbox 中使用 Chip,它是这样工作的。

<com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:flexWrap="wrap"
        app:justifyContent="center"> ... </com.google.android.flexbox.FlexboxLayout>

应该有更好的方法来实现这一点,但我想这会在那里起作用。

更新(2021):由于 Google 的稳定性和缺乏更新,我删除了对 flexbox 的依赖,并且现在使用 ConstraintLayout 的 Flow 实现了相同的效果,任何使用该技术的人也许也可以考虑,有一个查看 以编程方式填充它。

此库可帮助您解决查询问题。请检查 it.and 让我知道你的想法。

ChipCloud 库最初是 fiskurgit 为一些更大的黑客马拉松项目(非常)快速构建的 Android 视图。它创建了一个 material 'Chips' 的环绕云。他们版本的基本演示可以在 Play 商店中找到——我正在维护这个分支以获得我需要的功能。

Chip Cloud View

将其放入一个LinearLayout中并使其引力"center_horizontal"。然后设置芯片组的layout_width为"wrap_content"。这对我有用。

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_horizontal">


                <com.google.android.material.chip.ChipGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/langsChipGroup"
                    >

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

</LinearLayout>

我正在写信,认为它会对某人有所帮助。我有一个类似的问题。但是我有一个单行。我用下面的形式解决了这个问题

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

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.chip.ChipGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/padding"
            android:paddingEnd="@dimen/padding"
            app:singleLine="true">

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

//          ...

         </com.google.android.material.chip.ChipGroup>
     </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

如果我帮助了别人,我很高兴。

如果您的 ChipGroupConstraintLayout 中,请使用

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

这样:

<com.google.android.material.chip.ChipGroup
        android:id="@+id/chips"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
...

请确保不要将 layout_width 字段设置为 match_parent,因为这样不会起作用。