HAHHAHAH
```
public class FloatViewextends ViewGroup {
private Contextcontext;
public FloatView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
```
//摆放位置 布局
```
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
int space=10;
int left=0;
int right=0;
int top=0;
int bottom=0;
//获取控件总个数
int childCount = getChildCount();
//循环子控件
for (int j =0; j < childCount; j++) {
View childAt = getChildAt(j);
//测量宽高
childAt.measure(0,0);
//获取测量的宽度,这个宽度是在测量阶段结束之后才可以获取到
int measuredHeight = childAt.getMeasuredHeight();
int measuredWidth = childAt.getMeasuredWidth();
left=right+space;//left表示下一个文本内容进来开始位置
right=left+measuredWidth;//计算最后一个文本的右边大小
//获取父控件的宽度
int width=getWidth();//也就是屏幕的宽
if(right>width){
left=space;
top=bottom+space;
}
right=left+measuredWidth; //加载最后的文本内容的具体位置
bottom=top+measuredHeight;
//摆放位置
childAt.layout(left,top,right,bottom);
}
}
//初始化标签
public void addTag(String content){
TextView textView =new TextView(context);
textView.setTextSize(14);//字体大小
textView.setTextColor(Color.GREEN);//颜色
textView.setText(content);
textView.setGravity(Gravity.CENTER);//居中
textView.setBackgroundResource(R.drawable.style);//drawable在数据上添加一个圈
addView(textView);
}
}
//deawable数据加一个圈进行美化
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#d9c9c9" />
<corners android:radius="10dp" />
</shape>
@Override
//需要先和获取控件
public void onClick(View v) {
switch (v.getId()) {
case R.id.So_Btn:
String sp = Edit_Text.getText().toString().trim();
if (sp.isEmpty()) {
Toast.makeText(this, "请输入参数", Toast.LENGTH_SHORT).show();
} else {
//包含的意思
if (!mList.contains(sp)) {
Float.addTag(sp);
mList.add(sp);//string类型数组存入数据
} else {
Toast.makeText(this, "参数重复", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/So_Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
<EditText
android:id="@+id/Edit_Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入商品" />
</LinearLayout>
<com.example.day_09_zdy_xia.weight.FloatView3
android:id="@+id/Float"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp" />
</LinearLayout>
```