自定义view头部搜索

本文详细介绍了一种自定义视图的方法,通过继承LinearLayout并创建MyView类,实现了包含按钮和输入框的顶部布局。此外,文章展示了如何在Activity中使用自定义视图,并与RecyclerView结合,为用户提供搜索和扫一扫的功能。

1.top.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="horizontal">
<Button
    android:id="@+id/btn_sao"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="扫一扫"
    android:layout_alignParentLeft="true"
    />
<EditText
    android:id="@+id/ed_sou"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:hint="输入你想要搜索的东西"
    android:layout_marginLeft="30dp"
    />
<Button
    android:id="@+id/btn_sou"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="搜索"

    />
</LinearLayout>

2.创建MyView类继承Linerlayout

public class MyView extends LinearLayout {

            private  Button btn_sao,btn_sou;
            private EditText ed_sou;

            public MyView(Context context) {
                super(context);
            }
            //加载布局
            public MyView(Context context, @Nullable AttributeSet attrs) {
                super(context, attrs);
                View view = LayoutInflater.from(context).inflate(R.layout.top, this);
                btn_sao=view.findViewById(R.id.btn_sao);
                btn_sou=view.findViewById(R.id.btn_sou);
                ed_sou=view.findViewById(R.id.ed_sou);
                btn_sou.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = ed_sou.getText().toString();


                    }
                });


    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
}

3.将MyView文件应用到show页面的布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".view.ShowActivity"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        >
        <com.example.qq3.app.MyView
        android:id="@+id/myview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.example.qq3.app.MyView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rec"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></android.support.v7.widget.RecyclerView>

    </LinearLayout>


</LinearLayout>

4.在showActivity中获取资源id

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值