流式布局

开始之前准备几个文件夹

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"/>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值