//在输入框中,输入内容添加到数据库,使用流式布局展示,共两个数据库。头部布局是自定义组件。首先看一下效果
点击添加两个数据都添加内容,并展示;
点击删除按钮,可以删除最近搜索的内容,但常用分类不变。
//现在开始代码 //首先是头部布局的xml文件的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/send_sou"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher_round"
android:hint="搜索"
android:layout_weight="6"/>
<Button
android:id="@+id/send_add"
android:textSize="25sp"
android:text="添加"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
//然后就是自定义组件,创建一个类继承父布局
public class HeaderView extends LinearLayout {
private final EditText msend_sou;
private final Button msend_add;
//重写有两个参数的有参构造
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
//查找视图,及控件
LayoutInflater.from(context).inflate(R.layout.header,this);
msend_sou = findViewById(R.id.send_sou);
msend_add = findViewById(R.id.send_add);
}
//定义一个方法,获取输入框中的信息
public String getName(){
return msend_sou.getText().toString().trim();
}
//给添加按钮设置点击事件
public void setAdd(OnClickListener onClickListener){
msend_add.setOnClickListener(onClickListener);
}
}
//然后是自定义的流式布局的代码
public class LiuView extends LinearLayout {
private final int mParentWidth;
private int color;
private int textSizes;
public LiuView(Context context, AttributeSet attrs) {
super(contex