第一步 :布局
首先先自定义布局
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
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 float mText;
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context,AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
mText = array.getDimension(R.styleable.flow_textSize, 0);
}
public FlowLayout(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
mText = array.getDimension(R.styleable.flow_textSize, 0);
}
public void addTextView(String keys){
TextView view =(TextView) View.inflate(getContext(), R.layout.item, null);
view.setText(keys);
// view.setTextSize(TypedValue.COMPLEX_UNIT_PX,mText);
LayoutParams params=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
addView(view);
}
@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=20;
for (int i=0;i<getChildCount();i++){
View at = getChildAt(i);
int Vwidth = at.getWidth();
int Vheight = at.getHeight();
if (disWidth+Vwidth>width){
row++;
disWidth=20;
}
int i1 = row * Vheight + row * 20;
at.layout(
disWidth,i1,disWidth+Vwidth,i1+Vheight
);
disWidth+=(Vwidth+20);
}
}
}
values中添加attrs.xml文件
<resources>
<declare-styleable name="flow">
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>
自定义(shape)TextView边框
引用自己写的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/edit"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"/>
</LinearLayout>
<TextView
android:text="历史纪录"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<xf.com.week01.FlowLayout
android:id="@+id/flow_one"
android:textSize="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="热门搜索"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<xf.com.week01.FlowLayout
android:textSize="50dp"
android:id="@+id/flow_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="apple"
android:background="@drawable/shap"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="iphone"
android:background="@drawable/shap"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="华为"
android:background="@drawable/shap"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="小米"
android:background="@drawable/shap"
android:textSize="20sp" />
</xf.com.week01.FlowLayout>
<Button
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="OnClick"
android:text="清除历史纪录"/>
</LinearLayout>
主方法设置点击事件
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText edit;
private Button button;
private FlowLayout flow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = findViewById(R.id.edit);
button = findViewById(R.id.button);
flow = findViewById(R.id.flow_one);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.button){
String string = edit.getText().toString();
flow.addTextView(string);
}else{
flow.removeAllViews();
}
}
}
简单的流式布局就写完了
shape 边框在drawable中新建布局文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="@color/colorAccent"
android:width="5dp"></stroke>
</shape>
在TextView中设置background设置为自己写的shape边框就设置好啦
接下来就是嵌套数据库
第一步;创建数据库(这个大家都会)代码如下
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Sqlite extends SQLiteOpenHelper {
public Sqlite(Context context) {
super(context, "user", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(personid integer primary key autoincrement,json test)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
然后写数据库中的四大方法(增删改查)代码如下
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.annotation.RequiresApi;
public class SqlDao {
Context context;
private final SQLiteDatabase dao;
public SqlDao(Context context) {
this.context = context;
Sqlite sqlite=new Sqlite(context);
dao = sqlite.getWritableDatabase();
}
public long insertData(String table, String nullColumnHack, ContentValues values) {
return dao.insert(table, nullColumnHack, values);
}
public int deleteData(String table, String whereClause, String[] whereArgs){
return dao.delete(table,whereClause,whereArgs);
}
public Cursor queryData(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy){
return dao.query(table,columns,selection,selectionArgs,groupBy,having,orderBy);
}
}
我就写了三个方法没写修改可以根据自己的个人要求来写增删改查.这个并不是固定的
引用数据库调用方法
查询(如下)
Cursor user = dao.queryData("user", null, null,null, null, null, null);
if (user.moveToFirst()){
do {
String json = user.getString(user.getColumnIndex("json"));
mFlayout.addTextView(json);
}while(user.moveToNext());
}else{
Toast.makeText(MainActivity.this,"数据库无数据!",Toast.LENGTH_SHORT).show();
}
增加(如下)
ContentValues values=new ContentValues();
values.put("json",s);
dao.insertData("user",null,values);
删除(如下)
dao.deleteData("user",null,null);
添加增删改查到自己的项目中就OK了
数据库的流式布局就完成啦