写一个流式布局

在这里插入图片描述
效果图是这样

先写一个布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/edit_keys"
        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>
<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:paddingLeft="20dp"
    android:textSize="20sp"
    android:gravity="center_vertical"
    android:text="搜索历史"/>
<com.example.m2.FlowLayout
    android:id="@+id/flow_history_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

</com.example.m2.FlowLayout>
<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:paddingLeft="20dp"
    android:textSize="20sp"
    android:gravity="center_vertical"
    android:text="热门搜索"/>
<com.example.m2.FlowLayout
    android:id="@+id/flow_hot_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="apple"
        android:background="@drawable/car_btn_bg"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="iphone"
        android:background="@drawable/car_btn_bg"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="华为"
        android:background="@drawable/car_btn_bg"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="小米"
        android:background="@drawable/car_btn_bg"
        android:textSize="20sp" />
</com.example.m2.FlowLayout>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:text="清除历史记录"/>

MainActivity页面
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private FlowLayout flowLayout;
private Button mSearch;
private EditText mEdit;
private FlowDatabase mFlowDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mEdit = findViewById(R.id.edit_keys);
    mSearch = findViewById(R.id.btn_search);
    flowLayout = findViewById(R.id.flow_history_layout);
    mSearch.setOnClickListener(this);

    //新建一个数据库
    mFlowDB = new FlowDatabase(this);
    //查询搜索历史
    List<String> data = mFlowDB.query();
    for (int i=0;i<data.size();i++){

        //流失布局添加历史
        flowLayout.addTextView(data.get(i));
    }
}

@Override
public void onClick(View v) {
    //这个是点击搜索按钮
        if (v.getId()==R.id.btn_search){
            String keys = mEdit.getText().toString();
            //数据库插入搜索历史
            mFlowDB.insert(keys);

            //流失布局添加搜索内容
            flowLayout.addTextView(keys);
        }else {
            //移除所有的view
            flowLayout.removeAllViews();
            //数据库清空所有数据
            mFlowDB.delete();
        }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值