1. 程式人生 > >流式佈局,搜尋歷史

流式佈局,搜尋歷史

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>
在這裡插入程式碼片