流失布局 自定义view

#流失布局 自定义view

public class LiuView extends FrameLayout {
    private float mText;
    public LiuView(Context context) {
        this(context,null);
    }

    public LiuView(Context context,AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    private void init(Context context,AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LiuView);
        typedArray.getDimension(R.styleable.LiuView_textsize,0);
    }


    public  void addTextView(String name){
        TextView view =(TextView) View.inflate(getContext(), R.layout.liu_item, null);
        view.setText(name);

        LayoutParams params=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        addView(view);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int pwidth = getWidth();
        int row=0;
        int disWidth=20;
        for (int i=0;i<getChildCount();i++){
            View at = getChildAt(i);
            int Childwidth = at.getWidth();
            int Childheight = at.getHeight();
            if (disWidth+Childwidth>pwidth){
                row++;
                disWidth=20;
            }
            int i1 = row * Childheight + row * 20;
            at.layout(
                    disWidth,i1,disWidth+Childwidth,i1+Childheight
            );
            disWidth+=(Childwidth+20);
        }
    }
}

#布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/edit"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:drawableLeft="@mipmap/ic_launcher"
            />
        <Button
            android:id="@+id/button"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:textSize="20dp"
            android:text="搜索"/>
    </LinearLayout>

    <TextView
        android:text="历史纪录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.example.yuekaomn_20190118.view.LiuView
        android:id="@+id/liu_one"
        android:textSize="50dp"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="连衣裙"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />
    </com.example.yuekaomn_20190118.view.LiuView>
    <TextView
        android:text="热门搜索"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.example.yuekaomn_20190118.view.LiuView
        android:textSize="50dp"
        android:id="@+id/flow_two"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="11111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="111111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="1111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="1111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="1111141111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="1111141111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="1111141111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="111"
            android:background="@drawable/shape_liu"
            android:textSize="20sp" />
    </com.example.yuekaomn_20190118.view.LiuView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_liu"
        android:text="清除历史纪录"/>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值