流式布局,搜索历史

1.public class FlowLayout extends FrameLayout {
public FlowLayout( Context context) {
super(context);
}

public FlowLayout( Context context,  AttributeSet attrs) {
    super(context, attrs);
}

public FlowLayout( Context context,  AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

//效果参照:https://www.jianshu.com/p/05954091c650
@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 row = 0;//行数

    int disWidth = 20;//子控件左边的坐标

    for (int i = 0; i <getChildCount() ; i++) {

        View view = getChildAt(i);//查找子控件

        int viewWidth = view.getWidth();//子控件的宽度
        int viewHeight = view.getHeight();//子控件的高度

        if (disWidth+viewWidth>width){//接下来的控件的右边坐标超过了屏幕宽度
            row++;//行数增加
            disWidth = 20;//还原左边坐标
        }

        //第一个参数是左边坐标,第二个参数是上边坐标
        //左坐标应该是每行子控件宽度的总和disWidth
        //右坐标为左坐标+子控件宽度
        //上坐标应该是行数*控件高度
        //下坐标是上坐标+控件高度=(行数+1)*控件高度
        view.layout(disWidth,row*viewHeight,
                disWidth+viewWidth,viewHeight*(row+1));//子控件布局

        disWidth+=viewWidth;//记录下一次子控件左边的坐标

    }
}
  1. 布局

<com.dingtao.shopcar.FlowLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="apple"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="iphoneX"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="平板电脑手机"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="vivo"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="apple"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="apple"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="apple"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="oppo"
        android:textSize="20sp" />
</com.dingtao.shopcar.FlowLayout>
在这里插入代码片

猜你喜欢

转载自blog.csdn.net/weixin_43703093/article/details/84642652