首先,我们新建一个类
package com.qh.***.lmx1607flowlayout4;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FlowLayout extends FrameLayout {
private final static int H_DISTANCE = 20;//水平间距是20
private final static int V_DISTANCE = 20;//竖直间距是20
//自定义属性 字体的大小
private float mTextSize;
public FlowLayout(@NonNull Context context) {
super(context);
}
public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//自定义属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.flow);
mTextSize = typedArray.getDimension(R.styleable.flow_textSize, 25);
}
public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
mTextSize = array.getDimension(R.styleable.flow_textSize, 25);//得到的值单位为像素px
}
//添加文字控件的方法
public void addTextView(String name) {
//引入布局文件
TextView textView = (TextView) View.inflate(getContext(), R.layout.flow_item, null);
//设置文本
textView.setText(name);
//自定义属性 设置文字大小
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);
//布局宽高自适应
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//控件设置参数
textView.setLayoutParams(params);
addView(textView);
}
//摆放
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//获取本控件的宽度,用于计算换行
int width = getWidth();
//行数
int row = 0;
//子控件左边的坐标
int disWidth = H_DISTANCE;
for (int i = 0; i < getChildCount(); i++) {
//查找子控件
View view = getChildAt(i);
//子控件的宽度、高度
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
//Log.i("xzm", "textHeight:" + viewHeight);
//控件的右边坐标超过了屏幕宽度
if (disWidth + viewWidth > width) {
//行数增加
row++;
//还原左边坐标
disWidth = H_DISTANCE;
}
//左坐标应该是每行子控件宽度的总和disWidth
//右坐标为左坐标+子控件宽度
//上坐标应该是行数*控件高度
//下坐标是上坐标+控件高度=(行数+1)*控件高度
int viewTop = row * viewHeight + row * V_DISTANCE;
//子控件布局
view.layout(disWidth, viewTop,
disWidth + viewWidth, viewTop + viewHeight);
//记录下一次子控件左边的坐标
//disWidth += (viewWidth + H_DISTANCE);
disWidth = disWidth + (viewWidth + H_DISTANCE);
}
}
}
本类所使用的布局文件 flow_item(layout)-------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
android:background="@drawable/car_btn_bg"/>
car_btn_bg(drawable)------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="5dp"
android:color="@android:color/holo_red_dark">
</stroke>
</shape>
其次,设置我们的主布局和MainActivity
布局-----------------------------------------------------
<?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">
<EditText
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/btn_search"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="搜索" />
</LinearLayout>
<com.qh.***.lmx1607flowlayout4.FlowLayout
android:id="@+id/flow_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</com.qh.***.lmx1607flowlayout4.FlowLayout>
<Button
android:id="@+id/btn_delAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除历史记录" />
</LinearLayout>
MainActivity-----------------------------------------------------
package com.qh.xuezhimin.lmx1607flowlayout4;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.qh.xuezhimin.lmx1607flowlayout4.sqlite.DBHelper;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEditName;
/**
* 搜索
*/
private Button mBtnSearch;
private FlowLayout mFlowLayout;
/**
* 清除历史记录
*/
private Button mBtnDelAll;
private DBHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mEditName = findViewById(R.id.edit_name);
mBtnSearch = findViewById(R.id.btn_search);
mBtnSearch.setOnClickListener(this);
mFlowLayout = findViewById(R.id.flow_layout);
mBtnDelAll = findViewById(R.id.btn_delAll);
mBtnDelAll.setOnClickListener(this);
//初始化数据库
helper = new DBHelper(this);
//历史搜索查询
List<String> data = helper.query();
for (int i = 0; i <data.size() ; i++) {
//流式布局添加历史
mFlowLayout.addTextView(data.get(i));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_search:
String username = mEditName.getText().toString();
//数据库插入搜索历史
helper.insert(username);
//流式布局添加搜索内容
mFlowLayout.addTextView(username);
break;
case R.id.btn_delAll:
//数据库清空所有数据
helper.delete();
mFlowLayout.removeAllViews();
break;
}
}
}
最后,咱们大boss即将来临(SQLite)历史记录的保存
package com.qh.***.lmx1607flowlayout4.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
private final static String TABLE_NAME = "search";
private final SQLiteDatabase db;
public DBHelper(Context context) {
super(context, "flow.db", null, 1);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(_id integer primary key autoincrement,"
+ "names text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//添加数据
public void insert(String names) {
ContentValues values = new ContentValues();
values.put("names", names);
db.insert(TABLE_NAME, null, values);
}
//删除数据
public void delete() {
db.delete(TABLE_NAME, null, null);
}
//查询数据
public List<String> query() {
Cursor cursor = db.query(TABLE_NAME, null, null,
null, null, null, null);
List<String> list = new ArrayList<>();
while (cursor.moveToNext()) {
String names = cursor.getString(cursor.getColumnIndex("names"));
list.add(names);
}
return list;
}
}
哈哈哈,还有一个attrs哦,不过不用担心,只需要一个简单的文件即可
------------------记得这个atrrs.xml是values文件下的
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="flow">
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>