kaoshi_title_layout.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="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/left_btn" android:layout_width="50dp" android:layout_height="wrap_content" /> <TextView android:layout_weight="1" android:gravity="center" android:text="11111" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/right_btn" android:layout_width="50dp" android:layout_height="wrap_content" /> </LinearLayout>
activity_main.xml
<?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="match_parent" android:orientation="vertical"> <com.day04.TitleView android:id="@+id/titleview_id" android:layout_width="match_parent" android:layout_height="wrap_content"></com.day04.TitleView> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="@color/colorAccent" /> </LinearLayout>
TitleView.java
package com.day04; import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; /** * Created by lenovo on 2017/11/03. */ public class TitleView extends LinearLayout { public TitleView(Context context) { this(context, null); } public TitleView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // setContentView View inflate = LayoutInflater.from(context).inflate(R.layout.kaoshi_title_layout, this); Button left = (Button) inflate.findViewById(R.id.left_btn); Button right = (Button) inflate.findViewById(R.id.right_btn); left.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { callBack.leftClick(); } }); right.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { callBack.rightClick(); } }); } public LeftCallBack callBack; public void setCallBack(LeftCallBack callBack) { this.callBack = callBack; } interface LeftCallBack { public void leftClick(); public void rightClick(); } }
MainActivity.java
package com.day04; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity implements TitleView.LeftCallBack { public TitleView titleView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); titleView = (TitleView) findViewById(R.id.titleview_id); titleView.setCallBack(this); } @Override public void leftClick() { Toast.makeText(this, "leftClick", Toast.LENGTH_SHORT).show(); } @Override public void rightClick() { Toast.makeText(this, "rightClick", Toast.LENGTH_SHORT).show(); } }