如何使用 android 支持库中的 Chips 组件?

How to use Chips component from android support library?

我已阅读 official website 上的文档。但是我无法按照文档在我的项目中实现 Chip。 Android Studio 无法找到并导入 Chip class 以及 Chip 视图。

我还注意到 Google Developer site 上没有芯片 class 的参考。

还有一些类似的问题。但所有答案都指向使用第三方库。但我正在尝试使用 android 支持库中的 Chips 组件。

该功能包含在支持库版本 28.0.0-alpha1 中。
要使用该功能:

在应用 gradle 文件中:

android {
compileSdkVersion 'android-P'
}

dependencies {
  implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
  
  implementation 'com.android.support:design:28.0.0-alpha1'
  // OR
  implementation 'com.android.support:design-chip:28.0.0-alpha1'
}

在布局文件中:

<android.support.design.chip.Chip
    style="@style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:chipIcon="@drawable/ic_arrow_drop_down_black_24dp"
    app:chipText="hello"/>

准则:https://material.io/guidelines/components/chips.html

如果您使用的是 material 库

可以使用material芯片com.google.android.material.chip.Chipimplementation 'com.google.android.material:material:1.0.0'加入build.gradle

Filter style="@style/Widget.MaterialComponents.Chip.Filter"

Choice Chips style="@style/Widget.MaterialComponents.Chip.Choice"

Entry input: style="@style/Widget.MaterialComponents.Chip.Entry"

refer