attrs
<?xml version="1.0" encoding="utf-8"?><declare-styleable name="GroupDemoView">
<attr name="textColor" format="string"/>
</declare-styleable>
头部布局
<Liearout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/Search_Edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:drawableLeft="@mipmap/ic_launcher_round"
android:drawablePadding="5dp"/>
<TextView
android:id="@+id/Cancel_Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="添加" />
package com.example.weekone;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyXHView extends LinearLayout {
private EditText Search_Edit;
private TextView Cancel_Text;
public MyXHView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.header_view, this);
Search_Edit = findViewById(R.id.Search_Edit);
Cancel_Text = findViewById(R.id.Cancel_Text);
}
public String getEditStr(){
return Search_Edit.getText().toString();
}
public TextView getmCancel(){
return Cancel_Text;
}
}
主布局
<?xml version="1.0" encoding="utf-8"?><com.example.weekone.MyXHView
android:layout_width=“match_parent”
android:layout_height=“80dp”
android:id="@+id/header_View"></com.example.weekone.MyXHView>
<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.weekone.MyFlowLayoyut
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.weekone.MyFlowLayoyut
android:id="@+id/MyFloat_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textColor="@color/colorPrimaryDark" />
主页面
package com.example.weekone;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import sql.MyDao;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private String[] data = {“迪迪迪小公主了”, “可爱大方美丽善良简直可爱死了啦啦啦可爱大方美丽善良简直可爱死了啦啦啦可爱大方美丽善良简直可爱死了啦啦啦可爱大方美丽善良简直可爱死了啦啦啦”, “臭弟弟魏文成”, “老爸一点也不可爱”, “迪迪最可爱了”, “吕新新奇丑无比”, “火火勉强可爱吧”, “吕新新最自恋没有之一”, “还老说自己帅”, “啦啦啦啦啦啦”, “你说可爱就可爱了”, “小狗娃”, “笑口常开”, “揍你”, “哈哈哈”, “美丽”, “善良”, “大方”, “可爱”, “吕新新奇丑无比”, “什么玩意”};
private MyXHView header_View;
private TextView Delete_Text;
private MyFlowLayoyut MyFloat_Layout_History;
private MyFlowLayoyut MyFloat_Layout;
private ArrayList mList = new ArrayList<>();
private ArrayList mHistory = new ArrayList<>();
private MyDao myDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDao=new MyDao(this);
initData();
initView();
ArrayList<String> data = myDao.selName();
MyFloat_Layout_History.setData(data);
if (!mHistory.isEmpty()) {
MyFloat_Layout_History.setData(mHistory);
}
}
private void initData() {
for (int i = 0; i < data.length; i++) {
mList.add(data[i]);
}
}
private void initView() {
header_View = (MyXHView) findViewById(R.id.header_View);
Delete_Text = (TextView) findViewById(R.id.Delete_Text);
Delete_Text.setOnClickListener(this);
MyFloat_Layout_History = (MyFlowLayoyut) findViewById(R.id.MyFloat_Layout_History);
MyFloat_Layout = (MyFlowLayoyut) findViewById(R.id.MyFloat_Layout);
MyFloat_Layout.setData(mList);
header_View.getmCancel().setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Cancel_Text:
String s =header_View.getEditStr().trim();
myDao.insert(header_View.getEditStr().trim());
MyFloat_Layout_History.removiewchildView();
mHistory.add(s);
MyFloat_Layout_History.setData(mHistory);
break;
case R.id.Delete_Text:
myDao.delete();
MyFloat_Layout_History.removiewchildView();
break;
}
}
}
流式布局
package com.example.weekone;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MyFlowLayoyut extends LinearLayout {
private int mSreenWith;
private int mSreenHeight;
private final String mcolor;
public MyFlowLayoyut(Context context, AttributeSet attrs) {
super(context, attrs);
//获取屏幕的宽
DisplayMetrics metrics=context.getResources().getDisplayMetrics();
mSreenWith = metrics.widthPixels;
mSreenHeight= metrics.heightPixels;
setOrientation(VERTICAL);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupDemoView);
mcolor = (String) typedArray.getText(R.styleable.GroupDemoView_textColor);
}
@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();
for (int i=0;i<data.size();i++){
final String tmp=data.get(i);
int numWith=0;
int childCount = linearLayout.getChildCount();
for (int j = 0; j < childCount; j++) {
//通过index得到每一个子控件
TextView tv = (TextView) linearLayout.getChildAt(j);
LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
//测量这个tv的高和宽
tv.measure(getMeasuredWidth(), getMeasuredHeight());
numWith += tv.getMeasuredWidth() + leftMargin + tv.getPaddingLeft() + getPaddingRight();
}
for (int j = 0; j < childCount; j++) {
//通过index得到每一个子控件
TextView tv = (TextView) linearLayout.getChildAt(j);
LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
//测量这个tv的高和宽
tv.measure(getMeasuredWidth(), getMeasuredHeight());
numWith += 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 = 10;
params.topMargin = 10;
dataText.setLayoutParams(params);
dataText.setText(tmp);
dataText.measure(getMeasuredWidth(), getMeasuredHeight());
int dataTextWidth = dataText.getMeasuredWidth() + dataText.getPaddingLeft() + dataText.getPaddingRight();
if (mSreenWith>= numWith + dataTextWidth) {
Log.d("zzz",mSreenWith+","+numWith + dataTextWidth);
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.MATCH_PARENT);
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 void removiewchildView() {
removeAllViews();
}
}
数数据库
package sql;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import java.util.ArrayList;
public class MyDao {
private Context mcontext;
private final MyOpenHelper helper;
private final SQLiteDatabase database;
public MyDao(Context context){
mcontext=context;
helper = new MyOpenHelper(context);
database = helper.getWritableDatabase();
}
public void insert(String name){
ContentValues cv=new ContentValues();
cv.put("name",name);
database.insert("dd",null,cv);
Toast.makeText(mcontext, "插入成功", Toast.LENGTH_SHORT).show();
}
public ArrayList<String> selName(){
ArrayList<String> list=new ArrayList<>();
Cursor cursor=database.query("dd",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(){
database.execSQL("delete from dd");
}
}
创建表
package sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper{
public MyOpenHelper(Context context) {
super(context, “dddd”, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql= "create table dd(id Integer primary key autoincrement,name text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}