是否可以将 Material ChipGroup 方向设置为 VERTICAL?
Is it possible to set Material ChipGroup orientation to VERTICAL?
我正在将 material ChipGroup 动态添加到设置为垂直方向的父线性布局,但芯片项目似乎是水平添加的。有什么办法让它垂直排列芯片项目?
setLayoutDirection() ChipGroup 方法 class 方法似乎没有采用任何支持垂直方向的参数。
不,不能使用 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。
你可以在每个 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>
final layout
<?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
您可以将 chipSpacingHorizontal
参数设置为大于假定屏幕宽度的高值,以垂直对齐 ChipGroup 项目。
在 ChipGroup 布局中:
app:chipSpacingHorizontal="3000dp"
动态:
chipGroup.setChipSpacingHorizontal(3000);
这里我使用 3000 作为我认为大于屏幕宽度的随机高数。它可以是任何其他数字。
我正在将 material ChipGroup 动态添加到设置为垂直方向的父线性布局,但芯片项目似乎是水平添加的。有什么办法让它垂直排列芯片项目?
setLayoutDirection() ChipGroup 方法 class 方法似乎没有采用任何支持垂直方向的参数。
不,不能使用 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。
你可以在每个 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>
final layout
<?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
您可以将 chipSpacingHorizontal
参数设置为大于假定屏幕宽度的高值,以垂直对齐 ChipGroup 项目。
在 ChipGroup 布局中:
app:chipSpacingHorizontal="3000dp"
动态:
chipGroup.setChipSpacingHorizontal(3000);
这里我使用 3000 作为我认为大于屏幕宽度的随机高数。它可以是任何其他数字。