问题
I tried gravity="center"
, foregroundGravity="center"
and textAlignment="center"
for ChipGroup in the XML file but it won't work. Any ideas?
回答1:
I tried using Chip inside Flexbox and it worked like this.
<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>
There should be better ways for achieving this but this will work there I guess.
回答2:
This library is help you for figure out your query. please check it.and let me know your thoughts for the same.
The ChipCloud library was originally a (very) quickly knocked up Android view for some larger hackathon project by fiskurgit. It creates a wrapping cloud of material ' Chips'. Basic demo of their version is available on the Play Store - I'm maintaining this fork for features I required in it.
Chip Cloud View
回答3:
Put it into a LinearLayout and make its gravity "center_horizontal". Then set the chip group's layout_width to "wrap_content". This worked for me.
<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>
回答4:
You can go by creating one parent RelativeLayout and for each chipgroup use android:layout_centerHorizontal="true"
, something like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@android:color/darker_gray"
android:text="View1" />
<TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_toRightOf="@id/view1"
android:background="@android:color/darker_gray"
android:text="View2" />
</RelativeLayout>
</RelativeLayout>
回答5:
Put the width of chip group into wrap_content
and make the parent view to gravity to center
which contains chip group. So that it will be aligned center.
回答6:
Please try following :
layout_gravity="center"
Hope this will help you.
来源:https://stackoverflow.com/questions/51199787/android-how-to-center-align-chips-in-chipgroup