首页 文章

android支持库中的芯片组件? [关闭]

提问于
浏览
28

This材料设计展示案例说明芯片组件 . 但是我找不到这个组件的示例代码?

我怎么用呢?

请告诉我XML代码和java代码 .

3 回答

  • 53

    试试这个图书馆:https://github.com/klinker41/android-chips

    检查样品以了解如何使用它 .

  • 4

    您无需使用第三方库来制作chips .

    芯片基本上是 TextView ,背景为圆角 . 您还可以添加删除按钮和 ImageView 来创建视图,例如用于Google联系人的视图 .

    出于示例的目的,我将仅使用简单的 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>
    

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

    <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" />
    

    这是创建的视图:

    Chip view created

    现在我们可以使用 HorizontalScrollView 显示一排芯片(如GooglePlay应用程序)或使用 StaggeredGridLayoutManager 来构建交错的芯片网格 .

    [编辑]

    如果你想将芯片显示为 FlowLayout ,这在Android中并非固有支持,我们需要使用库 . 别担心,这是一个非常小的变化 .

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

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

    并使用它设置您的Recycler视图布局管理器:

    recyclerView.setLayoutManager(new FlowLayoutManager());
    

    有关GitHub的其他详细信息

    祝你有美好的一天!如果它有帮助就做upvote .

  • 4

    我知道我迟到了,但这可以帮助其他人 . 我真的很喜欢这个实现https://github.com/robertlevonyan/materialChipView . 此lib需要至少 com.android.support:appcompat-v7:25.2.0 的支持库 . 我在这个布局管理器https://github.com/BelooS/ChipsLayoutManager中使用它 . 检查这些,看看它们是否符合您的要求 .

相关问题