android - android支持库中的芯片组件?

标签 android material-design android-chips

This Material 设计展示案例介绍了芯片组件。但是我找不到这个组件的示例代码?

我该如何使用它?

请给我看一下 XML 代码和 java 代码。

最佳答案

您不需要使用第 3 方库来制作 chips .

芯片基本上是一个带有圆形背景的TextView。您还可以添加一个删除按钮和一个 ImageView 来创建一个 View ,例如用于 Google 联系人的 View 。

出于示例的目的,我将只使用一个简单的 TextView。首先为芯片创建 UI。

-> shape_chip_drawable.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/colorPrimary" />

    <padding
        android:bottom="12dp"
        android:left="12dp"
        android:right="12dp"
        android:top="12dp" />

    <corners android:radius="30dp" />
</shape>

在您的 Activity 布局文件中,只需将此可绘制资源文件定义为 TextView 背景,如下所示:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/shape_chip_drawable"
    android:text="Hello, I'm a simple Chip!"
    android:textColor="@android:color/white"
    android:textStyle="bold" />

这是创建的 View :

Chip view created

现在我们可以使用 Horizo​​ntalScrollView 显示一行筹码(如 GooglePlay 应用程序)或使用 StaggeredGridLayoutManager 构建一个交错的筹码网格。

[编辑]

如果您想将芯片显示为 FlowLayout,Android 本身并不支持,我们需要使用库。别担心,这是一个很小的变化。

您已在 Gradle 依赖项中添加以下行:

compile 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'

并使用它设置您的回收站 View 布局管理器:

recyclerView.setLayoutManager(new FlowLayoutManager());

更多详情请点击 GitHub

祝你有美好的一天!如果有帮助,请点赞。

关于android - android支持库中的芯片组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563739/

相关文章:

javascript - 在 React Native 中获取 API 之前如何保存当前位置的经纬度坐标?

android: 初始屏幕上的空指针

android - 如何使用fab风格的钻石?

android - 如何使用 appcompat v7 创建卡片工具栏

android - 带有可拖动芯片的芯片组

javascript - 在 React Native Stack Navigator 中更改 Header 的高度不起作用

html - Internet Explorer 中的居中对齐偏移

Android如何在芯片组中居中对齐芯片?

android - 如何在不透明度的android中更改芯片背景和描边颜色

android - Android 上 html 页面中的链接不起作用