搜索栏

//MainActivity

package com.example.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.but);
        final SeachVIew seachVIew = findViewById(R.id.serch);
        final EditText editText = findViewById(R.id.edit_text);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = new TextView(MainActivity.this);
                textView.setText(editText.getText());
                textView.setTextColor(Color.BLACK);
                textView.setBackgroundResource(R.drawable.edit_text);
                seachVIew.addView(textView);
            }
        });
    }
}

//Search

package com.example.myapplication;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class SeachVIew extends LinearLayout {
    //记录子视图最高的高度
    private int mchildMaxHeight;
    //给每个子视图添加一个左右的边距
    private int mLeftandRightDistance;
    //设置每行的上下间距
    private int mTopandBootmDistance;

    public SeachVIew(Context context) {
        super(context);
    }

    public SeachVIew(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //得到子控件的宽度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        //得到子空间的高度
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        //测量子view的大小
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        //得到最高的高度
        findMaxChildMaxHeight();
        //初始化值
        int left=0, top=0;
        //循环所有的孩子
        int childCount = getChildCount();
        for(int i=0;i<childCount;i++){
            //得到每一个子view
            View childAt = getChildAt(i);
            //判断是否是一行的开头
            if(left!=0){
                //不是一行开头就判断当前行能否装下
                if(left+childAt.getMeasuredWidth()>sizeWidth){
                    top+=mchildMaxHeight+mTopandBootmDistance;
                    left=0;
                }
            }
            left+=childAt.getMeasuredWidth()+mLeftandRightDistance;
        }
    setMeasuredDimension(sizeWidth,(top+mchildMaxHeight)>sizeHeight?sizeHeight:top+sizeHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int left = 0 ,top = 0;
        //循环所有的孩子
        int childCount = getChildCount();
        for(int i=0;i<childCount;i++){
            View childAt = getChildAt(i);
            if(left!=0){
                if(left+childAt.getMeasuredWidth()>getWidth()){
                    top+=mchildMaxHeight+mTopandBootmDistance;
                    left=0;
                }
            }
            childAt.layout(left,top,left+childAt.getMeasuredWidth(),top+mchildMaxHeight);
            left+=childAt.getMeasuredWidth()+mLeftandRightDistance;
        }
    }

    public void   findMaxChildMaxHeight(){
        //默认最高为0
        mchildMaxHeight=0;
        //得到所有的子view的数量
      int childCount = getChildCount();
        for(int i=0;i<childCount;i++){
            //循环每个view
            View childAt = getChildAt(i);
            //子视图高度大于当前最高值则赋值最高值
            if(childAt.getMeasuredHeight()>mchildMaxHeight){
                mchildMaxHeight+=childAt.getMeasuredHeight();
            }
        }
  }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值