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

Android如何将芯片组中的芯片居中对齐?,android,android-layout,android-chips,Android,Android Layout,Android Chips,我在XML文件中为芯片组尝试了gravity=“center”、foregroundGravity=“center”和textAlignment=“center”,但都不起作用。有什么想法吗 请尝试以下操作: layout_gravity="center" 希望这会对您有所帮助。将芯片组的宽度放入包装内容中,并使父视图重心指向包含芯片组的中心。这样它就会对准中心。我试着在内部使用芯片,它的工作原理是这样的 <com.google.android.flexbox.FlexboxLayout

我在XML文件中为芯片组尝试了
gravity=“center”
foregroundGravity=“center”
textAlignment=“center”
,但都不起作用。有什么想法吗

请尝试以下操作:

layout_gravity="center"

希望这会对您有所帮助。

将芯片组的宽度放入
包装内容中
,并使父视图重心指向包含芯片组的
中心。这样它就会对准中心。

我试着在内部使用芯片,它的工作原理是这样的

<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):由于谷歌的稳定性和缺少更新,我删除了FRAMBOX依赖性,AM现在使用约束布局的流程实现了同样的效果,任何使用该技术的人都可以考虑,也可以用编程方式来填充它。

< P>帮助您解决查询。请检查一下,让我知道你的想法

ChipCloud库最初是fiskurgit为某个更大的hackathon项目(非常)快速构建的Android视图。它创建了一个包裹材料“碎片”的云。他们版本的基本演示可以在Play Store上获得——我正在维护这个fork,以获得我需要的功能


将其置于线性布局中,并使其重心“水平”。然后将芯片组的布局宽度设置为“包裹内容”。这对我有用

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

//          ...

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

如果您在
ConstraintLayout
中有
芯片组
,请使用

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\u width
字段设置为
match\u parent
,因为那样不行。

嗨,M\u droid,我能知道如何修复这个问题吗,我也在使用芯片组,芯片需要在中心对齐,请帮助我。你的代码在哪里?你试过什么,哪个图书馆?请列出所有细节。你解决了吗?对不起,还没有机会测试其中一个答案。当时我们停止使用芯片,几个月后,该项目被取消了。android:textAlignment=“center”现在正在工作,只需复制粘贴他们的库描述即可。你应该把例子和链接放在一起。是的@PratikButani你可以在库中检查例子。如果他们会删除例子或者删除这个链接怎么办?这就是为什么总是把代码和示例放在一起,这样每个人都能理解。谢谢你的解决方案!我终于抽空检查了你的答案,结果它成功了。但是不使用flexbox的解决方案会更好。不管怎样,现在我接受你的。它成功了,tnx