import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.moni_01.R;
public class MyTitleView extends LinearLayout{
private Button bt_left; //返回按钮
private TextView tv_title; //标题名称
private Button bt_right; //查询按钮
public MyTitleView(Context context) {
this(context,null);
}
public MyTitleView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this, true);
bt_left = (Button) findViewById(R.id.bt_left);
tv_title = (TextView) findViewById(R.id.tv_title);
bt_right = (Button) findViewById(R.id.bt_right);
}
//自定义接口点击事件
public void setOnRigltClickListener(OnClickListener listener){
bt_right.setOnClickListener(listener);
}
public void setOnLeftClickListener(OnClickListener listener){
bt_left.setOnClickListener(listener);
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyTitleView mview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mview = (MyTitleView) findViewById(R.id.include); mview.setOnRigltClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,User.class); startActivity(intent); } }); }
//引用MyTitleView方法
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mine="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.moni_01.view.MyTitleView
android:id="@+id/include"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
//MyTitleView的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mine="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#d5d5c9"
android:orientation="horizontal">
<Button
android:id="@+id/bt_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="返回"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:text="那些花儿"
android:textColor="#494444"
android:textSize="30sp"
/>
<Button
android:id="@+id/bt_right"
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/aa"
/>
</LinearLayout>
</RelativeLayout>