在 ChipGroup 中添加垂直分隔符 (Android)

Adding Vertical divider in ChipGroup (Android)

我正在尝试在芯片组中添加垂直分隔线以将主芯片与其他芯片分开。就像 YouTube:

我试过用这个方法添加。在 Activity:

<HorizontalScrollView
    android:id="@+id/hscroll_categories"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="10dp"
    android:scrollbars="none">
    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipgroup_categories"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:orientation="horizontal"
        app:singleSelection="true"
        app:selectionRequired="true"
        app:singleLine="true" />
</HorizontalScrollView>

item_chip_category.xml:

<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    app:chipStartPadding="18dp"
    app:chipEndPadding="18dp"
    app:chipMinHeight="40dp"
    android:textColor="@color/txt_category_chip_light"
    app:chipBackgroundColor="@color/bg_category_chip_light"
    app:chipStrokeWidth="1dp"
    app:chipStrokeColor="@color/stroke_category_chip_light"
    style="@style/Widget.MaterialComponents.Chip.Choice"
    android:textAppearance="?android:attr/textAppearance" />

vertical_div.xml:

<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="#424242"/>

并动态添加芯片和分频器:

Chip chip = (Chip) this.getLayoutInflater().inflate(R.layout.item_chip_category, null, false);

LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(5,5,5,5);
chip.setLayoutParams(layoutParams);
chip.setText(some_var);

//adding chip
chipgroup_categories.addView(chip);

//adding divider
View div = (View) this.getLayoutInflater().inflate(R.layout.vertical_div, null, false);
chipgroup_categories.addView(div);

//adding more chips using loop

输出:

在输出中,没有一行,只有空 space,我是不是遗漏了什么?任何有助于找到添加它的有效方法的帮助都将不胜感激。

Im trying to add a vertical divider in chipgroup to separate primary chip from other chips. Just like YouTube:

ChipGroup 应该只包含芯片元素;探索按钮和垂直线不应是芯片元素。

因此,您可以使用水平 LinearLayout 来简化它,其中包含:探索按钮、垂直线、ChipGroup 以及可选的包裹在 [=15 中的“发送反馈”按钮=] 喜欢 YouTube:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <HorizontalScrollView
        android:id="@+id/horizontal_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#212121"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_explore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:backgroundTint="#373737"
                android:drawableLeft="@drawable/ic_baseline_explore_24"
                android:drawableTint="#fff"
                android:text="Explore" />

            <View
                android:id="@+id/vertical_line"
                android:layout_width="1dp"
                android:layout_height="50dp"
                android:layout_marginHorizontal="16dp"
                android:background="#424242" />

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/chipgroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                app:chipSpacing="16dp"
                app:chipSpacingHorizontal="40dp"
                app:chipSpacingVertical="4dp"
                app:singleLine="true"
                app:singleSelection="true"/>

            <TextView
                android:id="@+id/btn_end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="8dp"
                android:text="SEND FEEDBACK"
                android:textColor="#2AA9E3"
                android:textSize="16sp" />

        </LinearLayout>
    </HorizontalScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

并以编程方式将芯片添加到芯片组,这里是一个演示:

ChipGroup chipGroup = findViewById(R.id.chipgroup);

String[] chipArr = new String[]{"Arabic", "English", "Hindu", "German", "French", "Italian", "Urdu", "Spanish"};

for (String text : chipArr) {
    Chip chip = new Chip(this);
    chip.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
    chip.setText(text);
    chipGroup.addView(chip);
}


for (Chip chip : chips) chipGroup.addView(chip);