自定义对话框 Dialog
myDialog代码
import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.os.Handler; import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; import cn.ename.cxw.R; public class Dialog_cxw { private String title; private Button btnOk, btnCancel; private String message; private Context mContext; private static Dialog dialog; private static TextView tv_title; private static TextView tv_msg; private static Button btn_ok; private static Button btn_cancle; private LayoutInflater inflater; private static View dialogView; private final static int ALTERDIALOG = 0; private final static int DIALOG = 1; private Handler dialogHandler; private Activity activity; public myDialog() { super(); // TODO Auto-generated constructor stub } public myDialog(Activity activity, String title, String message) { super(); this.title = title; this.message = message; this.activity=activity; this.mContext = activity.getApplicationContext(); } /** * 单个按钮对话框 * * @param activity * @param mContext * @param title * @param message * @param btnOk */ public static void showDialog(Activity activity, String title, String message, OnClickListener btnOk) { initView(activity.getApplicationContext()); initDialog(title, message, btnOk, null, DIALOG); dialog = new Dialog(activity, R.style.dialog); dialog.setContentView(dialogView); Window dialogWindow = dialog.getWindow(); WindowManager m = activity.getWindowManager();// Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用// WindowManager.LayoutParams p = activity.getWindow().getAttributes(); // 获取对话框当前的参数值// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6// p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.95// dialogWindow.setAttributes(p); dialog.show(); } /** * 两个按钮对话框 * * @param activity * @param mContext * @param title * @param message * @param btnOk */ public static void showAlertDialog(Activity activity, String title, String message, OnClickListener btnOk, OnClickListener btnCancel) { initView(activity.getApplicationContext()); initDialog(title, message, btnOk, btnCancel, ALTERDIALOG); dialog = new Dialog(activity, R.style.dialog); dialog.setContentView(dialogView); Window dialogWindow = dialog.getWindow();//自定义大小 WindowManager m = activity.getWindowManager();// Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用// WindowManager.LayoutParams p = activity.getWindow().getAttributes(); // 获取对话框当前的参数值// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6// p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.95// dialogWindow.setAttributes(p); dialog.show(); } /** * 初始化对话框的界面 * * @param mContext */ private static void initView(Context mContext) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE); // LayoutInflater inflater = // LayoutInflater.from(mContext.getApplicationContext()); dialogView = inflater.inflate(R.layout.alert_dialog, null); tv_title = (TextView) dialogView.findViewById(R.id.tv_dialog_title); tv_msg = (TextView) dialogView.findViewById(R.id.tv_dialog_msg); btn_ok = (Button) dialogView.findViewById(R.id.btn_dialog_ok); btn_cancle = (Button) dialogView.findViewById(R.id.btn_dialog_cancel); } /** * 初始化对话框的数据 * * @param title * @param message * @param btnOk * @param flag */ private static void initDialog(String title, String message, OnClickListener btnOk, OnClickListener btnCancel, int flag) { tv_title.setText(title); tv_msg.setText(message); if (flag == 0) {// 弹出两个个按钮对话框 btn_ok.setOnClickListener(btnOk); btn_cancle.setOnClickListener(btnCancel); } if (flag == 1) {// 弹出单个按钮对话框 btn_cancle.setVisibility(View.GONE); btn_ok.setOnClickListener(btnOk); } } public static void dialogDismiss(){ if (dialog!=null) { dialog.dismiss(); } } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFFFF" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/dialog_border" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/tv_dialog_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingTop="5dp" android:layout_marginLeft="1dp" android:layout_marginRight="1dp" android:gravity="center_vertical" android:background="@drawable/tip_title" android:text="@string/dialog_title" android:textColor="@color/wathet_blue_text" android:textSize="18sp" /> </LinearLayout> <TextView android:id="@+id/tv_dialog_msg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:gravity="center" android:text="@string/dialog_msg" android:textColor="@color/black" android:textSize="20sp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/btn_dialog_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:padding="5dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:background="@drawable/btn_gray" android:textSize="18sp" android:text="@string/dialog_ok" /> <Button android:id="@+id/btn_dialog_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:padding="5dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:background="@drawable/btn_gray" android:textSize="18sp" android:text="@string/dialog_cancel" /> </LinearLayout> </LinearLayout> </LinearLayout>
dialog_border.xml添加边框
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFFFFF" /> <stroke android:width="0.01dp" android:color="#CCCCCC" /> <padding android:bottom="1dp" android:left="0.5dp" android:right="0.5dp" android:top="0.5dp" /> </shape>
效果图