1. 程式人生 > >流式佈局(瀑布流,Sqlite資料庫,shape)

流式佈局(瀑布流,Sqlite資料庫,shape)

第一步 :佈局

首先先自定義佈局


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了

資料庫的流式佈局就完成啦