【发布时间】:2022-01-23 19:25:41
【问题描述】:
我试图在芯片组中添加一个垂直分隔线,以将主芯片与其他芯片分开。就像 YouTube 一样:
我已尝试通过此方法添加它。在活动中:
<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
输出:
在输出中,没有行,只有空格,我错过了什么吗?任何帮助找到添加它的有效方法表示赞赏。
【问题讨论】:
-
我同意 Zain 关于芯片组的使用。如果您想继续现有的,请尝试将调用的第二个参数设置为带有芯片组的充气机,以指定线的高度。如果您对现在发生的事情感到好奇,请使用 AS Layout Inspector 查看布局。
标签: android android-chips