**//自定义view**
package com.bawei.flow_layout;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
*
* @Data:2019/4/25 19:45
* 描述:
*/
public class Flow_Layout extends FrameLayout {
private int space=20;
// private List<String> list=new ArrayList<>();
public Flow_Layout(Context context) {
super(context);
}
public Flow_Layout(Context context,AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
space= (int) array.getDimension(R.styleable.flow_space,0);
}
public Flow_Layout(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
space= (int) array.getDimension(R.styleable.flow_space,0);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int width=getWidth();
int height=getHeight();
int rows=0;
int hwidth=0;
for (int i = 0; i < getChildCount(); i++) {
TextView view = (TextView) getChildAt(i);
//长按删除
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
post(new Runnable() {
@Override
public void run() {
removeView(v);
}
});
return false;
}
});
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 addview(String s){
// list.add(s);
TextView textView= (TextView) View.inflate(getContext(), R.layout.view_item, null);
textView.setText(s);
LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
addView(textView);
}
}
流试布局自定义view
最新推荐文章于 2022-09-07 20:32:36 发布