自定义布局引入

布局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"
    >

    <ImageView
        android:layout_width="@dimen/dp_20"
        android:layout_height="@dimen/dp_30"
        android:padding="0dp"
        android:id="@+id/headview_Image"

        android:src="@mipmap/common_nav_btn_menu"
        />
    
    <EditText
        android:layout_width="0dp"
        android:layout_height="@dimen/dp_20"
        android:layout_weight="4"
        android:hint="请输入要搜索的内容"
        android:id="@+id/headview_edit"
        android:layout_marginRight="@dimen/dp_10"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_marginTop="@dimen/dp_3"
        android:background="@drawable/head_view_stork"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="@dimen/dp_30"
        android:id="@+id/headview_search"
        android:text="搜索"
        android:textSize="@dimen/sp_10"
        android:layout_marginBottom="@dimen/dp_10"
        />

</LinearLayout>

MyHeaderView

package com.example.activity.myapplication.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.activity.myapplication.R;
import com.example.activity.myapplication.event.MyHeadEventBean;

import org.greenrobot.eventbus.EventBus;

public class MyHomeHeadView extends LinearLayout implements View.OnClickListener {
    private ImageView headview_image;
    private EditText headview_edit;
    private TextView headview_search;


    public MyHomeHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.home_head_view, this);
        initView();
    }

    private void initView() {

        headview_edit = findViewById(R.id.headview_edit);
        headview_image = findViewById(R.id.headview_Image);
        headview_search = findViewById(R.id.headview_search);

        headview_image.setOnClickListener(this);
        headview_search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.headview_Image:
                EventBus.getDefault().postSticky(new MyHeadEventBean(0,null));
                break;
            case R.id.headview_search:

                String name = headview_edit.getText().toString().trim();
              EventBus.getDefault().postSticky(new MyHeadEventBean(1,name));
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43668405/article/details/85943242