是否可以将 Material ChipGroup 方向设置为 VERTICAL?

Nia*_*med 5 android material-design android-chips

我正在将材料ChipGroup动态添加到设置为 VERTICAL 方向的父线性布局,但芯片项目似乎是水平添加的。有没有办法让它垂直放置芯片项目?

ChipGroup 类方法的setLayoutDirection()方法似乎没有采用任何支持垂直方向的参数。

小智 7

您可以在每个 Chip 项目中设置 width = match_parent

<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>
Run Code Online (Sandbox Code Playgroud)

最终布局


Zie*_*ony 1

不,不可能使用 ChipGroup。ChipGroup 扩展了一个简单的 FlowLayout,该布局旨在并排布局项目(然后在另一行中)。您可以在这里找到 ChipGroup:https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/chip/ChipGroup.java

FlowLayout 并没有那么复杂。如果我是你,我只会复制源代码并修改 FlowLayout.onLayout() 以垂直布局子项。如果您不需要额外的功能(例如选择跟踪),您可以只使用 FlowLayout 而无需修改 ChipGroup。如果您需要一个非常简单的芯片堆栈,您可以简单地使用垂直 LinearLayout。