一、添加依赖
compile 'com.nex3z:flow-layout:0.1.2'
二、使用
1.在Xml中声明:
<com.nex3z.flowlayout.FlowLayout
android:id="@+id/flow_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:childSpacing="6dp"
app:rowSpacing="8dp" />
<!--自定义属性-->
<!--
flow = "true" 流式布局
childSpacing:子控件之间的距离 "auto"
rowSpacing:行间距
childSpacingForLastRow:子控件最后一行的间距
rtl = "true" 一行从右往左一次排列
-->
2.给子控件设置shape
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff00ff"/>
<corners android:radius="25dp"/>
<stroke android:width="1dp" android:color="#3799f4"/>
</shape>
>
3.java代码中动态添加子控件
private void getList(List<ListBean> list) {
mFlowLayout = (FlowLayout) findViewById(R.id.flow_layout);
/**
* 根据集合动态生成控件TextView
*/
for (final ListBean b: list) {
TextView textView = new TextView(this);
textView.setPadding(16,8,16,8);
textView.setText(b.getName());
textView.setTextSize(16);
textView.setBackgroundResource(R.drawable.s);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v,b.getTitle(),Snackbar.LENGTH_SHORT).show();
}
});
mFlowLayout.addView(textView);
}
}
三、效果图