自定义view实现流式布局

创建一个类继承viewGroup

public class LiuShi extends ViewGroup {
    public LiuShi(Context context) {
        super(context,null);
    }

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

    public LiuShi(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void addchild(View view){
        if(view!=null){
            addView(view);
            invalidate();
            postInvalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int lineWidth = 0;
        int lineHeight = 0;
        int left = 0;
        int top = 0;
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            MarginLayoutParams layoutParam = (MarginLayoutParams) child.getLayoutParams();
            int cWidth = child.getMeasuredWidth() + layoutParam.leftMargin + layoutParam.rightMargin;
            int cHeight = child.getMeasuredHeight() + layoutParam.topMargin + layoutParam.bottomMargin;
            //换行
            if (cWidth + lineWidth > getMeasuredWidth()) {
                left = 0; top = lineHeight;
                child.layout(left + layoutParam.leftMargin, top + layoutParam.topMargin, left + layoutParam.leftMargin + child.getMeasuredWidth(), top + layoutParam.topMargin + child.getMeasuredHeight());
                left += layoutParam.leftMargin + child.getMeasuredWidth();
                lineWidth = cWidth; lineHeight += cHeight;
            } else {
                lineWidth += cWidth;
                lineHeight = Math.max(lineHeight, cHeight);
                child.layout(left + layoutParam.leftMargin, top + layoutParam.topMargin, left + layoutParam.leftMargin + child.getMeasuredWidth(), top + layoutParam.topMargin + child.getMeasuredHeight());
                left += layoutParam.leftMargin + child.getMeasuredWidth();
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //当前控件最终需要的高度
        int measureWidth=0; int measureHeight=0;
        // 获取侧栏模式的大小和宽度
         int measureWidthSize = MeasureSpec.getSize(widthMeasureSpec);
         int measureHeightSize = MeasureSpec.getSize(heightMeasureSpec);
         int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
         int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
         //测量子view
         int childCount = getChildCount();
         for (int i = 0; i < childCount; i++) {
             View child = getChildAt(i);
             measureChild(child, widthMeasureSpec, heightMeasureSpec);
             //测量子view
             MarginLayoutParams layoutParam = (MarginLayoutParams) child.getLayoutParams();
             //控件的宽度和左右间隔
             int cWidth = child.getMeasuredWidth() + layoutParam.leftMargin + layoutParam.rightMargin;
             int cHeight = child.getMeasuredHeight() + layoutParam.topMargin + layoutParam.bottomMargin;
             //换行
             if (measureWidth + cWidth > this.getMeasuredWidth()) {
                 measureWidth = Math.max(measureWidth, cWidth);
                 measureHeight += cHeight;
             } else {
                 //当前行
                 measureHeight = Math.max(measureHeight, cHeight); measureWidth += cWidth; } }
                 //把测量的子view的大小设置给当前控件
                 setMeasuredDimension(measureWidthMode == MeasureSpec.EXACTLY ? measureWidthSize : measureWidth, measureHeightMode == MeasureSpec.EXACTLY ? measureHeightSize : measureHeight);
             }
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new MarginLayoutParams(getContext(), attrs);
    }
}

创建布局xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:background="@drawable/yuan"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp">
//搜索的图片
    <ImageView
        android:id="@+id/sousuo_activity_image"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@mipmap/sousuo"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"/>
//搜索框
    <EditText
        android:id="@+id/sousuo_activity_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@null"
        android:layout_weight="9"
        android:hint="请输入想要搜索的商品"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="10dp"
        android:paddingLeft="10dp"/>
    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@mipmap/yvyin"
        android:layout_margin="8dp"/>
</LinearLayout>
//流式布局的控件
<com.example.acer.jd.com.bwie.view.custom.LiuShi
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/liuShi"></com.example.acer.jd.com.bwie.view.custom.LiuShi>

activity中的搜索图片的点击事件

sousuo_activity_image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //首先清空自定义view中的所有
        liuShi.removeAllViews();
//得到搜索框中的数据
        String sousuo = sousuo_acitivity_edit.getText().toString();
//在外面创建一个存放string类型数据的list集合将搜索框里的数据存进去
        list.add(sousuo);
//通过反向的循环将最后一条添加的数据展示到第一条
        for (int i = list.size()-1; i >-1 ; i--) {
//创建一个textview
            textView = new TextView(SouSuoActivity.this);
//为textview设置文字
            textView.setText(list.get(i));
            textView.setGravity(Gravity.CENTER); textView.setLayoutParams(layoutParams);
            liuShi.addchild(textView);
        }
//赋值完毕后将得到的搜索框里的内容携带跳转到展示搜索信息的页面
        Intent intent = new Intent(SouSuoActivity.this,ShowActivity.class);
        intent.putExtra("sousuo",sousuo);
//将搜索框还原为空
        sousuo_acitivity_edit.setText("");
        startActivity(intent);
    }
});

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值