【问题标题】:Is it possible to set Material ChipGroup orientation to VERTICAL?是否可以将 Material ChipGroup 方向设置为 VERTICAL?
【发布时间】:2019-02-05 08:23:25
【问题描述】:

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

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

【问题讨论】:

  • myCusstomView.setRotation(45);你试过了吗
  • setlayoutdirection 用于 rtl 设置。
  • @vikas kumar setRotation 旋转整个 ChipGroup 容器

标签: android material-design android-chips


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    不,不能使用 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。如果您需要一个非常简单的芯片堆栈,您可以简单地使用垂直线性布局。

    【讨论】:

    • 或者,您可以在项目中包含github.com/google/flexbox-layout,并使用带有许多Chip 子级的FlexboxLayout; FlexboxLayout 是一个更强大的 FlowLayout,允许您指定方向。
    • @Zielony 谢谢。我查找了 ChipGroup 的来源。我会采纳你的建议
    • @BenP。以前从未使用过 FlexBoxLayout。似乎很有用。以后会试试的。
    【解决方案3】:
    <?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

    【讨论】:

      【解决方案4】:

      您可以将chipSpacingHorizontal 参数设置为大于假定屏幕宽度的高值,以垂直对齐芯片组项目。
      在 ChipGroup 布局中:

      app:chipSpacingHorizontal="3000dp"
      

      动态:

      chipGroup.setChipSpacingHorizontal(3000);
      

      这里我使用 3000 作为我认为大于屏幕宽度的随机高数。它可以是任何其他数字。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多