android 自定義對話方塊
阿新 • • 發佈:2019-02-04
想要自己設計對話方塊的話
1、在xml中設計自己想要的樣式 在style中重寫主題
2、新建一個java檔案繼承dialog 重寫相應的方法
3、在現實的activity中呼叫自定義的對話方塊
演示效果:
下面是演示程式碼:
對話方塊的佈局檔案 xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/duty_dialog_style" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="3dp" android:gravity="center" android:text="值日提醒" android:textColor="#FFFFFF" android:textSize="18sp" /> <TextView android:id="@+id/tv_CleanInformation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="3dp" android:text="值日人員是:李林凱 龐田旺" android:textColor="#FFFFFF" android:textSize="16sp" /> <Button android:id="@+id/bt_sure" android:layout_width="wrap_content" android:layout_height="35dp" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:background="@drawable/duty_dialog_style" android:gravity="center" android:text="好的老大" android:textColor="#FFFFFF" android:textSize="13sp" /> </LinearLayout>
效果:
drawable中xml檔案:
style檔案:<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 顏色 --> <solid android:color="#60c325" /> <!-- 彎角 --> <corners android:radius="20dp" /> <padding android:bottom="10dp" android:left="5dp" android:right="5dp" android:top="5dp" /> <!-- 邊框 --> <stroke android:width="0.5dp" android:color="#FFFFFF" > </stroke> </shape>
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <!-- 重寫系統彈出Dialog --> <style name="myDialogTheme" parent="android:Theme.Dialog"> <item name="android:background">#ffffff</item> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <!-- 除去title --> <item name="android:windowContentOverlay">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@null</item> <!-- 除去背景色 --> </style> </resources>
對話方塊的java檔案
*
*
*@auther Jianjun Huang
*
*@date 2015年12月11日
*/
public class Duty_Dialog extends Dialog {
public Duty_Dialog(Context context) {
super(context);
}
public Duty_Dialog(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private String title;
private Context context;
private String message;
private String positiveButtonText;
private View contentView;
private DialogInterface.OnClickListener positiveButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* 設定資訊(string)
*
* @param message
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* 設定資訊(resource)
*
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
*
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
*
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
public Duty_Dialog create(){
//LayoutInflater作用是將layout的xml佈局檔案例項化為View類物件 通過SystemService獲得
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Duty_Dialog dialog = new Duty_Dialog(context, R.style.myDialogTheme);
View layout = inflater.inflate(R.layout.duty_dialog_style, null);
//添加布局
dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
((TextView) layout.findViewById(R.id.tv_title)).setText(title);
//設定按鈕
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.bt_sure)).setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.bt_sure)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
layout.findViewById(R.id.bt_sure).setVisibility(View.GONE);
}
if (message != null) {
((TextView) layout.findViewById(R.id.tv_CleanInformation)).setText(message);
}
else if (contentView != null) {
((LinearLayout) layout.findViewById(R.id.linearLayout_dialog)).removeAllViews();
((LinearLayout) layout.findViewById(R.id.linearLayout_dialog)).addView(contentView,
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
activity的java檔案
// 對話提醒
private void alterDiog(String cleanMember2, String weeks, String mWay) {
Duty_Dialog.Builder builder = new Duty_Dialog.Builder(MyApplication.getContext());
builder.setTitle("值日提醒");
// 設定資訊
builder.setMessage("今天是" + "\t" + weeks + "\t" + mWay + "\n" + "值日人員:" + cleanMember2 + "\n大家互相提醒一下!");
builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Window window = MyApplication.getWindow();
// 獲取對話方塊當前的引數值
// WindowManager.LayoutParams p = window.getAttributes();
//
// WindowManager m = MyApplication.getWindow().getWindowManager();
// // 獲取螢幕寬、高用
// Display d = m.getDefaultDisplay();
//
// // 寬度設定為螢幕的0.65
// p.width = (int) (d.getWidth() * 0.6);
//
// // 設定位置
// window.setGravity(Gravity.CENTER);
// // 透明度
// p.alpha = 0.7f;
// window.setAttributes(p);
Duty_Dialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//
// 將彈出框設定為全域性
dialog.setCanceledOnTouchOutside(false);// 失去焦點不會消失
dialog.show();
}
因為我的這個對話方塊要在全域性顯示所以才在最後加上了全域性顯示的部分
若不需要全域性顯示可以不用這樣寫 直接create後show就可以了