安卓材料设计中的“chips”指什么?

18
我想在我的应用中实现一个带有芯片的自动完成EditText,并且我希望以这里的方式完成:material design chips。首先,我想问是否有一些小部件(也许作为新支持库的一部分)或解决方案可供使用,以便轻松实现。 (我知道此问题以前已经被问过,但我只想知道是否有任何变化)。此外,我发现了这个,但是我不知道如何使用它(以及是否可以使用它)来自动完成我的数据集...之前有人使用过这个库吗?可以分享他们的经验吗?任何帮助都将不胜感激!
5个回答

9
新的Material Components for Android包含组件Chip
您可以使用以下代码将芯片添加到您的布局文件中:
<com.google.android.material.chip.Chip
    android:id="@+id/some_chip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is a chip" />

enter image description here

使用经典的Design Support Library 28.0.0,您可以使用以下软件包:

<android.support.design.chip.Chip
../>

您可以使用以下属性自定义组件:

  • android:checkable:如果为true,则芯片可以切换。如果为false,则芯片的行为类似于按钮。
  • app:chipIcon:用于在芯片内显示图标
  • app:closeIcon:用于在芯片内显示关闭图标

您可以在此处找到官方文档


1
我自己找到了这个,这正是很久以前所需要的。太好了,我们终于得到它了。谢谢! - Sandra
为什么 app:checkable 不起作用?我收到了“错误:找不到属性 checkable”的提示。 - Darush
@GabrieleMariotti 我使用了android:checkable而不是app:checkable,它起作用了。非常感谢您的任何想法。 - Darush

6

我最终使用了这个。虽然该库提供的自动完成视图和芯片没有"材料"样式,但您可以轻松自行设置。经过深入研究,我发现该库为您提供了实现自己数据集的最简单方法。所有其他库都是定制用于使用Android联系人或电子邮件,并且更改代码并查询自己的数据集并不那么容易。因此,如果有人想要实现芯片,但需要自定义数据查询,则我认为这是正确的方法。


1

非常感谢!我会尽快查看。 - Sandra

0
到目前为止最新的进展是.. 这个库 看起来非常棒且易于使用。你需要
implementation "com.hootsuite.android:nachos:1.1.1"

<com.hootsuite.nachos.NachoTextView
    android:id="@+id/nacho_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:chipHorizontalSpacing="2dp"
    app:chipBackground="@color/chip_background"
    app:chipTextColor="@color/cheddar"
    app:chipTextSize="16dp"
    app:chipHeight="30dp"
    app:chipVerticalSpacing="3dp"/>

val suggestions = arrayOf("Tortilla Chips", "Melted Cheese", "Salsa", "Guacamole", "Mexico", "Jalapeno")
val adapter = ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, suggestions)
nachoTextView.setAdapter(adapter)

玩转自定义!


-1

这些链接可能会有所帮助

Chip小部件提供了Material Design芯片组件的完整实现。以下是如何在布局中包含小部件的示例代码:

<com.google.android.material.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"/>

https://material.io/develop/android/components/chip/

另外的第三方库

Nachos是一个用于Android的库,提供自定义TextView以允许用户输入文本并在文本字段中创建材料芯片。 https://github.com/hootsuite/nachos

MaterialChipsInput 实现了Android的Material Design Chips组件。该库提供了两个视图:ChipsInput和ChipView。 https://github.com/pchmn/MaterialChipsInput

材料芯片视图。可用作类别、联系人或创建文本云的标签。 https://github.com/robertlevonyan/materialChipView


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接