该材料设计展示案例介绍了芯片组件。但是我找不到这个组件的示例代码?
我该如何使用它?
请给我看 XML 代码和 java 代码。
您不需要使用 3rd 方库来制作芯片。
芯片基本上是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
背景,如下所示:
<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" />
这是创建的视图:
现在我们可以HorizontalScrollView
用来显示一排筹码(如 GooglePlay 应用程序)或StaggeredGridLayoutManager
用来构建一个交错的筹码网格。
[编辑]
如果您想将芯片显示为FlowLayout
Android 本身不支持的芯片,我们需要使用库。别担心,这是一个很小的变化。
您已在 Gradle 依赖项中添加以下行:
compile 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
并使用它设置您的回收站视图布局管理器:
recyclerView.setLayoutManager(new FlowLayoutManager());
GitHub 上的其他详细信息
祝你有美好的一天!如果有帮助,请点赞。
试试这个库: https ://github.com/klinker41/android-chips
检查示例以了解如何使用它。
我知道我迟到了,但这可以帮助其他人。我也很喜欢这个实现https://github.com/robertlevonyan/materialChipView。这个库至少需要com.android.support:appcompat-v7:25.2.0
支持库。我将它与这个布局管理器一起使用https://github.com/BelooS/ChipsLayoutManager。检查这些,看看它们是否符合您的要求。