开始之前准备几个文件夹
1.attrs.xml
2.drawable文件夹下的car_btn_bg.xml文件
一共两个Activity
public class FlowLayout1 extends FrameLayout {
private FlowViewClickListener flowViewClickListener;
private int space;
private int textSize;
private String bg;
public FlowLayout1(Context context) {
super(context);
}
public FlowLayout1(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public FlowLayout1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
public void init(Context context,AttributeSet attrs) {
TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.flow);
space=(int)array.getDimension(R.styleable.flow_space,0);
textSize=(int)array.getDimension(R.styleable.flow_textSize,0);
bg = array.getString(R.styleable.flow_bg);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int width = getWidth();//获取控件宽度
int hWidth = 0;//记录每行控件累加的宽度
int rows = 0;//行数
for (int i = 0; i < getChildCount(); i++) {
final TextView view = (TextView) getChildAt(i);
view.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (flowViewClickListener != null) {
flowViewClickListener.flowViewClick((TextView) view);
}
}
});
int childWidth = view.getWidth();
int childHeight = view.getHeight();
hWidth = hWidth + (childWidth + space);
if (hWidth > width) {//如果累加宽度大于控件宽度,则换行
rows++;
hWidth = childWidth + space;//超过之后,直接等于子控件的宽度
}
view.layout(hWidth - childWidth, rows * childHeight + (rows + 1) * space,
hWidth, (rows + 1) * childHeight + (rows + 1) * space);
}
}
// 接口回调
public void setFlowViewClickListener(FlowViewClickListener flowViewClickListener) {
this.flowViewClickListener = flowViewClickListener;
}
public void addTextView(String s) {
TextView textView = (TextView) View.inflate(getContext(),R.layout.flow_item,null);
textView.setText(s);
//布局宽高自适应
LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);//控件设置上布局参数
addView(textView);
}
public interface FlowViewClickListener {
void flowViewClick(TextView view);
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FlowLayout1 mFlowLayout;
private EditText mEdit;
private Button mSearch;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdit = findViewById(R.id.edit_keys);
mSearch = findViewById(R.id.btn_search);
mSearch.setOnClickListener(this);
mFlowLayout = findViewById(R.id.flow_hot_layout);
mFlowLayout.setFlowViewClickListener(new FlowLayout1.FlowViewClickListener() {
@Override
public void flowViewClick(TextView view) {
view.setText("AAA");
}
});
}
@Override
public void onClick(View v) {
mFlowLayout.addTextView(mEdit.getText().toString());
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
android:background="@drawable/car_btn_bg"/>