以title控件为例

1.创建一个类继承LinearLayout

2.在activity.xml中引用:



<com.firstlinetestapp.ui.view.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" />



 3.编辑toolbar的控件事件:

class TitleLayout(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

init {
LayoutInflater.from(context).inflate(R.layout.title,this)

title_back.onClick {
val c = getContext() as Activity
c?.finish()
}

title_edit.onClick {
Toast.makeText(getContext(), "Clicked Edit button", Toast.LENGTH_SHORT).show()

}
}

}