gpt4 book ai didi

android - 在 ChipGroup (Android) 中添加垂直分隔线

转载 作者:行者123 更新时间:2023-12-04 23:48:14 24 4
gpt4 key购买 nike

我试图在芯片组中添加一个垂直分隔器以将主芯片与其他芯片分开。就像 YouTube 一样:
Image from 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
输出:
Output
在输出中,有 没有行,只有空白 ,我错过了什么吗?任何帮助找到添加它的有效方法表示赞赏。

最佳答案

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

ChipGroup应该只容纳芯片元素;探索按钮和垂直线不应该是芯片元素。
因此,您可以使用水平 LinearLayout 来简化它。有:探索按钮、垂直线、 ChipGroup ,以及可选的“发送反馈”按钮,包含在 HorizontalScrollView 中喜欢 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 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);

关于android - 在 ChipGroup (Android) 中添加垂直分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70451281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com