贴一张偷来的图

本文还要给流动标签加上彩色来美化这些标签
首先需要使用到的是洋神的FlowLayout库
Git地址
调用过程并不复杂,这里主要整理一下数据设置的方法,官方贴出的方法是内置一个字符串
mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
{
@Override
public View getView(FlowLayout parent, int position, String s)
{
TextView tv = (TextView) mInflater.inflate(R.layout.tv,
mFlowLayout, false);
tv.setText(s);
return tv;
}
});
但是大多数应用场景中,我们都需要大量标签来充斥这块空间
mFlowLayout.setAdapter(new TagAdapter<Bean>(Datas) {
@Override
public View getView(FlowLayout parent, int position, Bean bean) {
TextView tagView = (TextView) LayoutInflater.from(SearchActivity.this).inflate(R.layout.item_search_tag, mKeywordTagLayout, false);
tagView.setText(设置文本);
setTagTextColor(tagView); //设置标签颜色
return tagView;
}
});
这里采用list包裹的bean来填充数据,其中使用方法TagAdapter<模型>(数据),并且在getView中会把模型数据引入,这样就可以设置文本内容了。
那么如何给标签加上随机颜色呢?
private void setTagTextColor(TextView tagView) {
int red, green, blue;
Random mRandow = new Random();
red = mRandow.nextInt(255);
green = mRandow.nextInt(255);
blue = mRandow.nextInt(255);
int color = Color.rgb(red, green, blue);
tagView.setTextColor(color);
}
这里直接贴一段随机颜色的生成方法
这样的标签效果很好,实现步骤也不是很复杂。
顺手贴上item_search_tag
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="18sp"
android:background="@drawable/tag_shape">
</TextView>
tag_shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp"/>
<solid android:color="@color/_10000000"/>
</shape>