首先attr.xml
<resources>
<declare-styleable name="GroupDemoView">
<attr name="textColor" format="string" />
</declare-styleable>
</resources>
text_style:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#F3F3F3"/>
<corners android:radius="15dp"/>
</shape>
activity_main:
<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:background="#fff"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- 自定义的标题栏 -->
<com.example.zdy_float_day04.weight.MyXHView
android:id="@+id/header_View"
android:layout_width="match_parent"
android:layout_height="80dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="搜索历史" />
<TextView
android:id="@+id/Delete_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="删除" />
</RelativeLayout>
<com.example.zdy_float_day04.weight.MyFloatLayout
android:id="@+id/MyFloat_Layout_History"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textColor="@color/colorPrimaryDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="热门搜索" />
<com.example.zdy_float_day04.weight.MyFloatLayout
android:id="@+id/MyFloat_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textColor="@color/colorPrimaryDark" />
</LinearLayout>
header_view.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/Search_Edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="5dp"
android:hint="搜索" />
<TextView
android:id="@+id/Cancel_Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="添加" />
</LinearLayout>
public class MyFloatLayout extends LinearLayout {
private int mScreenWidth;
private int mScreenHeight;
private String mColor;
public MyFloatLayout(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mScreenWidth = metrics.widthPixels;
mScreenHeight = metrics.heightPixels;
//设置这个布局垂直显示
setOrientation(VERTICAL);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupDemoView);
if (typedArray != null) {
mColor = (String) typedArray.getText(R.styleable.GroupDemoView_textColor);
}
}
public void removeChildView() {
//移除所有子控件
removeAllViews();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
public void setData(ArrayList<String> data) {
LinearLayout linearLayout = getLin();//[lvxx,lxs,lzs,lzzs]
for (int i = 0; i < data.size(); i++) {//lvxx
final String tmp = data.get(i);
int numWidth = 0;
//得到一行LinearLayout到底有多少子控件 因为我要计算每个子控件加在一起的宽度
int childCount = linearLayout.getChildCount();
//这个for循环只是计算一行LinearLayout的所有子控件的宽的和
for (int j = 0; j < childCount; j++) {
//通过index得到每一个子控件
TextView tv = (TextView) linearLayout.getChildAt(j);
LayoutParams layoutParams = (LayoutParams) tv.get ayoutParams();
int leftMargin = layoutParams.leftMargin;
//测量这个tv的高和宽
tv.measure(getMeasuredWidth(), getMeasuredHeight());
numWidth += tv.getMeasuredWidth() + leftMargin + tv.getPaddingLeft() + getPaddingRight();
}
TextView dataText = getText();
dataText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), tmp, Toast.LENGTH_SHORT).show();
}
});
//设置属性
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = 15;
params.topMargin = 10;
dataText.setLayoutParams(params);
dataText.setText(tmp);
dataText.measure(getMeasuredWidth(), getMeasuredHeight());
int dataTextWidth = dataText.getMeasuredWidth() + dataText.getPaddingLeft() + dataText.getPaddingRight();
//考虑到一个字符串很长 就直接超过整个屏幕的高了
// if (dataTextWidth>=mScreenWidth){
// String s = tmp.substring(0, 4);
// dataText.setText(s+"...");
// dataText.measure(getMeasuredWidth(), getMeasuredHeight());
// dataTextWidth = dataText.getMeasuredWidth();
// }
if (mScreenWidth >= numWidth + dataTextWidth) {//lvxx
linearLayout.addView(dataText);
} else {
//这里面对LinearLayout重新赋值 通过getLin换行
linearLayout = getLin();
linearLayout.addView(dataText);
}
}
}
//初始化子LinearLayout
private LinearLayout getLin() {
LinearLayout linearLayout = new LinearLayout(getContext());
//LayoutParams 控制组件大小的一个工具类
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
//this本类对象
this.addView(linearLayout);//只要重新添加View了自动换行了
return linearLayout;
}
//初始化TextView
private TextView getText() {
TextView textView = new TextView(getContext());
textView.setTextSize(20);
textView.setTextColor(Color.parseColor(mColor));
textView.setBackgroundResource(R.drawable.text_style);
textView.setPadding(10, 3, 10, 3);
return textView;
}
}
public class MyXHView extends LinearLayout {
private EditText mEditText;
private TextView mCancel;
public MyXHView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.header_view, this);
mEditText = findViewById(R.id.Search_Edit);
mCancel = findViewById(R.id.Cancel_Text);
}
public String getEditStr() {
return mEditText.getText().toString();
}
public TextView getmCancel() {
return mCancel;
}
}
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "lvxx.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table liu(id Integer primary key autoincrement,name text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MyDao {
private MyOpenHelper helper;
private SQLiteDatabase mData;
private Context mContext;
public MyDao(Context context) {
mContext = context;
helper = new MyOpenHelper(context);
mData = helper.getWritableDatabase();
}
public void insertSqlite(String name) {
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
mData.insert("liu", null, contentValues);
Toast.makeText(mContext, "插入成功", Toast.LENGTH_SHORT).show();
}
public ArrayList<String> selectName() {
ArrayList<String> list = new ArrayList<>();
Cursor cursor = mData.query("liu", null, null, null, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
list.add(name);
}
return list;
}
public void delete() {
mData.execSQL("delete from liu");
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String[] data = {"流感", "咳嗽", "过敏", "发烧", "感冒", "湿疹", "便秘", "痔疮", "协和", "鼻炎", "失眠", "痛风", "上火", "脚气", "抑郁症", "性欲", "乳腺增生", "头晕", "腰痛"};
private MyFloatLayout MyFloat_Layout;
private MyFloatLayout mHistoryLayout;
private MyXHView mHeadView;
private MyDao mDao;
private ArrayList<String> mList = new ArrayList<>();
private ArrayList<String> mHistory = new ArrayList<>();
private TextView mDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDao = new MyDao(this);
mHistory = mDao.selectName();
initData();
initViews();
if (!mHistory.isEmpty()) {
mHistoryLayout.setData(mHistory);
}
}
private void initData() {
for (int i = 0; i < data.length; i++) {
mList.add(data[i]);
}
}
private void initViews() {
mDelete = findViewById(R.id.Delete_Text);
mDelete.setOnClickListener(this);
MyFloat_Layout = findViewById(R.id.MyFloat_Layout);
MyFloat_Layout.setData(mList);
mHistoryLayout = findViewById(R.id.MyFloat_Layout_History);
mHeadView = findViewById(R.id.header_View);
mHeadView.getmCancel().setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Cancel_Text:
String name = mHeadView.getEditStr().trim();
mDao.insertSqlite(mHeadView.getEditStr().trim());
//自己封装了一个方法删除子控件
mHistoryLayout.removeChildView();
mHistory.add(name);
mHistoryLayout.setData(mHistory);
break;
case R.id.Delete_Text:
mDao.delete();
mHistoryLayout.removeChildView();
break;
}
}
}