摘要:Android - 客製化 Dialog
public static Dialog createDialogOKCancle(Activity activity,String msg,View.OnClickListener callbackOk,View.OnClickListener callbackCancle,boolean showCancle) {
Dialog dialog = new Dialog(activity,R.style.CustomDialog);//指定自定義樣式
dialog.setContentView(R.layout.dialog_custom);//指定自定義layout
// 取得螢幕解析度
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int vWidth = dm.widthPixels;
int vHeight = dm.heightPixels;
//可自由調整佈局內部元件的屬性
LinearLayout ll = (LinearLayout)dialog.findViewById(R.id.lldialog);
ll.getLayoutParams().width=(int)(vWidth*0.9);
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
lp.y = (int)(vWidth*0.4); // 新位置Y坐標
/*lp.x = 500; // 新位置X坐標
lp.y = 450; // 新位置Y坐標
lp.width = 100; // 寬度
lp.height = 100; // 高度
lp.alpha = 0.7f; // 透明度*/
//新增自定義按鈕點擊監聽
Button btnOk = (Button)dialog.findViewById(R.id.dialog_button_ok);
btnOk.setOnClickListener(callbackOk);
Button btnCancle = (Button)dialog.findViewById(R.id.dialog_button_cancel);
btnCancle.setOnClickListener(callbackCancle);
if(!showCancle) {
LinearLayout ll_butotn = (LinearLayout)dialog.findViewById(R.id.lldialog_button);
ll_butotn.getLayoutParams().width = (int)(ll.getLayoutParams().width * 0.7);
btnCancle.setVisibility(View.GONE);
btnOk.setWidth((int)(ll.getLayoutParams().width*0.5));
}
TextView txtMessage = (TextView)dialog.findViewById(R.id.message);
txtMessage.setText(msg);
//顯示dialog
dialog.setCancelable(false);
return dialog;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
>
<LinearLayout
android:id="@+id/lldialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/message"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lldialog_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:paddingBottom="10dp" >
<Button
android:id="@+id/dialog_button_cancel"
android:layout_width="0dp"
android:layout_height="51dip"
android:layout_weight="1"
android:background="@color/dialog_button"
android:text="取消"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/dialog_button_ok"
android:layout_width="0dp"
android:layout_height="51dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@color/dialog_button"
android:text="確定"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>