自定义标题控件,使标题控件上的按钮有了触发事件
package com.example.uicustomviews; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; /** * Created by Administrator on 2016/3/1. */ public class TitleLayout extends LinearLayout implements View.OnClickListener{ public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, this); Button title_back = (Button)findViewById(R.id.title_back); Button title_edit = (Button)findViewById(R.id.title_edit); title_back.setOnClickListener(this); title_edit.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.title_back: ((Activity)getContext()).finish(); break; case R.id.title_edit: Toast.makeText(getContext(),"This is editing button",Toast.LENGTH_SHORT).show(); break; default: break; } } }