流试布局自定义view

本文介绍了一种自定义的FlowLayout布局实现方法,通过继承FrameLayout并重写onLayout方法,实现了视图的动态布局和长按删除功能。文章详细解释了如何获取子View的宽度和高度,以及如何计算和设置子View的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**//自定义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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值