Is it possible to set Material ChipGroup orientation to VERTICAL?

試著忘記壹切 提交于 2021-02-15 06:19:58

问题


I'm adding a material ChipGroup dynamically to a parent linear layout which is set to VERTICAL orientation but the chip items seem to be added horizontally. Is there any way to make it lay out the chip items vertically?

setLayoutDirection() method of ChipGroup class method doesn't seem to take any parameter that supports VERTICAL orientation.


回答1:


you can set width = match_parent in each Chip item

<com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/questionTxt">

        <com.google.android.material.chip.Chip
            android:id="@+id/opt1"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Fire" />

        <com.google.android.material.chip.Chip
            android:id="@+id/opt2"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Water" />

        <com.google.android.material.chip.Chip
            android:id="@+id/opt3"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Psychic" />

        <com.google.android.material.chip.Chip
            android:id="@+id/opt4"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Psychic" />
    </com.google.android.material.chip.ChipGroup>

final layout




回答2:


No, it's not possible using ChipGroup. ChipGroup extends a simple FlowLayout, which was designed to layout items side to side (and then in another line). You can find ChipGroup here: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/chip/ChipGroup.java

FlowLayout is not that complex. If I were you, I would just copy the sources and modify FlowLayout.onLayout() to layout children vertically. If you don't need extra features (like selection tracking), you can just use FlowLayout without modifying ChipGroup. If you need a really simple stack of chips, you can simply use a vertical LinearLayout.



来源:https://stackoverflow.com/questions/54530257/is-it-possible-to-set-material-chipgroup-orientation-to-vertical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!