//搜索布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <Button android:id="@+id/backto" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/ret" /> <EditText android:id="@+id/editext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="20" android:hint="请输入" /> <Button android:id="@+id/serach" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/ser" /> </LinearLayout>
//HeadView类
public class HeadView extends LinearLayout { private Button backto; private Button serch; private EditText edtext; public HeadView(Context context) { super(context); info(context); } public HeadView(Context context, AttributeSet attrs) { super(context, attrs); info(context); } public HeadView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); info(context); } private void info(Context context) { View inflate = LayoutInflater.from(context).inflate(R.layout.headview, null); backto = inflate.findViewById(R.id.backto); serch = inflate.findViewById(R.id.serach); edtext = inflate.findViewById(R.id.editext); serch.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { String s = edtext.getText().toString(); dataCallBack.getdata(s); } }); } DataCallBack dataCallBack; public void SetDataCallBack(DataCallBack dataCallBack){ this.dataCallBack=dataCallBack; } public interface DataCallBack{ void getdata(String data); }
//实现添加View
public class serchView extends ViewGroup { Context context; private int maxHeight; private int marginleft = 20; private int margintop = 20; public serchView(Context context) { super(context); } public serchView(Context context, AttributeSet attrs) { super(context, attrs); } public serchView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onLayout(boolean b, int i, int i1, int i2, int i3) { //找到最大高度方法 findMaxHeight(); //定义左边距 int left = 0; //定义上边距 int top = 0; //获取子View的个数 int childCount = getChildCount(); //循环最大的子View的个数 for (int j = 0; j <getChildCount() ; j++) { //获取子View View childAt = getChildAt(j); //获取子View的宽度 int measuredWidth = childAt.getMeasuredWidth(); //父View的宽度 int width = getWidth(); //当左边距不是第一个的时候,需要判断左边距和子view宽度 // 是否超过父View的长度,否则换行 if (left!=0){ //如果超过就换行 if(left+measuredWidth>width){ //换行 将上边距加上最大高度,让子View高度变高,来实现换行 top = top+maxHeight+margintop; left = 0; } } //不超过则把左边距加上子view的宽度即可,bottom是上边距加上子view的高度 childAt.layout(left,top,left+measuredWidth,top+maxHeight); //每次出现一个子View,大左边距都需要变化,将左边距加上子View宽度和 left = left+measuredWidth+marginleft; } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //测量父view的宽度 int widthsize = MeasureSpec.getSize(widthMeasureSpec); int heightsize = MeasureSpec.getSize(heightMeasureSpec); measureChildren(widthMeasureSpec,heightMeasureSpec); //测量MaxHeight的值 findMaxHeight(); int left = 0; int top = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View view = getChildAt(i); int measuredWidth = view.getMeasuredWidth(); //如果为第一个则不用增加下边距,加左边距即可 if(left != 0){ //如果ziview加上左边距大于父View的宽度则换行 if((left+measuredWidth)>widthsize){ //让高度变高,即可能实现换行,下边距加上ziview高度即可 //并把左边距赋值为0,从左边开始添加子View top = top+maxHeight+margintop; left = 0; } } left = left+measuredWidth+marginleft; } //想要实现自己View的宽高大小,必须实现这个方法 //宽度不用变,高度需要改变,高度是根据添加的行数高度来改变 heightsize=top+maxHeight; setMeasuredDimension(widthsize,heightsize); } private void findMaxHeight(){ maxHeight = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View childAt = getChildAt(i); //获取子View的高度 int measuredHeight = childAt.getMeasuredHeight(); //如果变量子View最大高度小于子View的高度, // 则将子View高度赋值给MaxHeight if(maxHeight<measuredHeight){ maxHeight = measuredHeight; } } } }
//activity中实现
setContentView(R.layout.activity_serch); headView = findViewById(R.id.headview); serch = findViewById(R.id.serch); //添加搜索的内容 headView.setCallBack(new HeadView.EditCallBack() { @Override public void editcallback(String name) { TextView textView = new TextView(SerchActivity.this); textView.setText(name); textView.setTextColor(Color.WHITE); textView.setTextSize(20); textView.setBackgroundColor(Color.BLACK); serch.addView(textView); } });