流式布局

本文深入解析了自定义FlowLayout控件的实现细节,包括如何通过XML属性设置间距、文字大小及背景,以及如何实现流式布局效果。文章还介绍了如何为控件添加点击监听器,并实现了动态添加TextView的方法。

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

package movie.bw.com.week3;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FlowLayout extends FrameLayout {

private int space;
private int textsize;
private TypedArray array;
private String bg;
private FlowListener flowListener;
public FlowLayout(Context context) {
    super(context);
}

public FlowLayout(Context context,AttributeSet attrs) {
    super(context, attrs);
    array = context.obtainStyledAttributes(attrs, R.styleable.flow);
    space = (int) array.getDimension(R.styleable.flow_space,0);
    textsize = (int) array.getDimension(R.styleable.flow_testSize, 0);
    bg = array.getString(R.styleable.flow_bg);


}

public FlowLayout(Context context,AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    array = context.obtainStyledAttributes(attrs, R.styleable.flow);
    space = (int) array.getDimension(R.styleable.flow_space,0);
    textsize = (int) array.getDimension(R.styleable.flow_testSize, 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++){
        TextView view= (TextView) getChildAt(i);
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX,textsize);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flowListener!=null){
                    flowListener.flowclick(view);
                }
            }
        });
        int childwidth = view.getWidth();
        int childheight = view.getHeight();
        hWidth= hWidth+(childwidth+space);//如果累加宽度大于控件宽度 就换行
        if (hWidth>width){
            rows++;
            hWidth= (int) (childwidth+space);//超过之后等于子控件宽度
        }
        view.layout(hWidth-childwidth,rows*childheight+(rows+1)*space, hWidth, (rows + 1) * childheight + (rows + 1) * space);
    }
}
public void setFlowLayout(FlowListener flowListener) {
    this.flowListener = flowListener;
}
public void addView(String s){
    TextView view = (TextView) View.inflate(getContext(), R.layout.flow_item, null);
    view.setText(s);

// 布局自适应
LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(layoutParams);
addView(view);
}
// 定义接口
public interface FlowListener{
void flowclick(TextView view);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值