MainActivyty.java
package com.example.uimyview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity{
protected void onCreate(Bundle save) {
super.onCreate(save);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
TitleLayout.java
package com.example.uimyview;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
public class TitleLayout extends LinearLayout {
private ButOnClickListener listener;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(getContext()).inflate(R.layout.title, this);
listener = new ButOnClickListener();
Button back = (Button) findViewById(R.id.back);
Button edit = (Button) findViewById(R.id.edit);
back.setOnClickListener(listener);
edit.setOnClickListener(listener);
}
private class ButOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back:
((Activity) getContext()).finish();
break;
case R.id.edit:
AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
dialog.setTitle("提示");
dialog.setMessage("确定退出?");
dialog.setCancelable(false);
dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Activity)getContext()).finish();
}
});
dialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// ProgressDialog dialog = new ProgressDialog(getContext());
// dialog.setTitle("提示");
// dialog.setMessage("正在加载....");
dialog.show();
break;
}
}
}
}
activity_main.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" >
<com.example.uimyview.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.uimyview.TitleLayout>
</LinearLayout>
title.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:background="@drawable/title_bg"
android:orientation="horizontal" >
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/back_bg"
android:text="@string/back"
android:textColor="#fff" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="This is title"
android:textColor="#fff"
android:textSize="24sp" />
<Button
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>