6

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

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

4

3 回答 3

6

您可以在每个 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>

最终布局

于 2019-09-04T14:23:16.327 回答
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。如果您需要一个非常简单的芯片堆栈,您可以简单地使用垂直线性布局。

于 2019-02-06T22:47:43.507 回答
0
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipsGrp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/screen_pd"
        android:padding="@dimen/screen_pd"
        app:chipSpacing="@dimen/screen_pd"
        app:singleSelection="true" />

</ScrollView>

在 Scrollview 中包含您的 ChipGroup

于 2021-09-20T05:06:42.140 回答