流式布局 简易版

Main

package bawei.wang.demo_flowlayout01;

import android.content.ContentValues;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import bawei.wang.demo_flowlayout01.sql.Dao;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edtext;
    private FlowLayout flowLayout;
    private Dao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dao = new Dao(MainActivity.this);
        edtext = findViewById(R.id.editText);
        Button b1 = findViewById(R.id.button);
        Button b2 = findViewById(R.id.btn_clear);
        flowLayout = findViewById(R.id.myFlowLayout);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);

        Cursor query = dao.query("d1130", null, null, null, null, null, null);
        if (query.moveToFirst()) {
            do{
                String s = query.getString(query.getColumnIndex("s"));
                flowLayout.addViewaa(s);
            }while (query.moveToNext());
        }else{
            Toast.makeText(this, "数据库无数据", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                String trim = edtext.getText().toString().trim();
                flowLayout.addViewaa(trim);
                ContentValues values = new ContentValues();
                values.put("s",trim);
                long insert = dao.insert("d1130", null, values);
                Log.e("添加",""+insert);
                break;

            case R.id.btn_clear:
                edtext.getText().clear();
                flowLayout.removeAllViews();
                int delete = dao.delete("d1130", null, null);
                Log.e("删除",delete+"");
                break;
        }
    }
}

自定义viwe

package bawei.wang.demo_flowlayout01;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FlowLayout extends FrameLayout {
    public FlowLayout(Context context) {
        super(context);

    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    // 点击搜索后  将输入框的数据添加进去
    public void addViewaa(String keys) {
        // 加载布局
        TextView textView = (TextView) View.inflate(getContext(), R.layout.flow_item_text, null);

        // 设置文字
        textView.setText(keys);

        // 设置文字自适应
        FrameLayout.LayoutParams f = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(f);


        // 添加视图
        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 = 20;// 子控件左边的距离

        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);// 找到子控件
            // 获取子控件的宽度
            int viewWidth = view.getWidth();
            // 获取子控件的高度
            int viewHeight = view.getHeight();

            // 如果所有子控件的宽度相加超过了屏幕宽度,则换行进行展示
            if (disWidth + viewWidth > width) {
                row++;// 行数加一行
                disWidth = 20; // 使下一行的第一个控件距离左边相等
            }

            // 设置位置
            view.layout(disWidth, row * viewHeight + row * 20, disWidth + viewWidth, (row * viewHeight + row * 20) + viewHeight);
            //记录下一次子控件的坐标
            disWidth += (viewWidth + 20);

        }
    }
}

Mian.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName"
            android:hint="输入即存入下方"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜索" />
    </LinearLayout>

    <bawei.wang.demo_flowlayout01.FlowLayout
        android:id="@+id/myFlowLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </bawei.wang.demo_flowlayout01.FlowLayout>

    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清空" />
</LinearLayout>

控件Text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="尝试一下"
    android:textSize="20sp"
    android:padding="10dp"
    android:textColor="#00f"
    android:background="@drawable/text_b">

</TextView>

猜你喜欢

转载自blog.csdn.net/qq_41972756/article/details/84727788