仿ios自定义Dialog
1.自定义的CustomDialog.java
1 package com.example.iosalertview; 2 3 import android.app.Dialog; 4 import android.content.Context; 5 import android.content.DialogInterface; 6 import android.view.Gravity; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup.LayoutParams; 10 import android.widget.Button; 11 import android.widget.LinearLayout; 12 import android.widget.TextView; 13 14 public class CustomDialog extends Dialog { 15 16 public CustomDialog(Context context) { 17 super(context); 18 } 19 20 public CustomDialog(Context context, int theme) { 21 super(context, theme); 22 } 23 24 public static class Builder { 25 private Context context; // 上下文对象 26 private String title; // 对话框标题 27 private String message; // 对话框内容 28 private String confirm_btnText; // 按钮名称“确定” 29 private String cancel_btnText; // 按钮名称“取消” 30 private String neutral_btnText; // 按钮名称“隐藏” 31 private View contentView; // 对话框中间加载的其他布局界面 32 /* 按钮坚挺事件 */ 33 private DialogInterface.OnClickListener confirm_btnClickListener; 34 private DialogInterface.OnClickListener cancel_btnClickListener; 35 private DialogInterface.OnClickListener neutral_btnClickListener; 36 37 public Builder(Context context) { 38 this.context = context; 39 } 40 41 /* 设置对话框信息 */ 42 public Builder setMessage(String message) { 43 this.message = message; 44 return this; 45 } 46 47 /** 48 * Set the Dialog message from resource 49 * 50 * @param title 51 * @return 52 */ 53 public Builder setMessage(int message) { 54 this.message = (String) context.getText(message); 55 return this; 56 } 57 58 /** 59 * Set the Dialog title from resource 60 * 61 * @param title 62 * @return 63 */ 64 public Builder setTitle(int title) { 65 this.title = (String) context.getText(title); 66 return this; 67 } 68 69 /** 70 * Set the Dialog title from String 71 * 72 * @param title 73 * @return 74 */ 75 public Builder setTitle(String title) { 76 this.title = title; 77 return this; 78 } 79 80 /** 81 * 设置对话框界面 82 * 83 * @param v 84 * View 85 * @return 86 */ 87 public Builder setContentView(View v) { 88 this.contentView = v; 89 return this; 90 } 91 92 /** 93 * Set the positive button resource and it's listener 94 * 95 * @param confirm_btnText 96 * @return 97 */ 98 public Builder setPositiveButton(int confirm_btnText, 99 DialogInterface.OnClickListener listener) { 100 this.confirm_btnText = (String) context.getText(confirm_btnText); 101 this.confirm_btnClickListener = listener; 102 return this; 103 } 104 105 /** 106 * Set the positive button and it's listener 107 * 108 * @param confirm_btnText 109 * @return 110 */ 111 public Builder setPositiveButton(String confirm_btnText, 112 DialogInterface.OnClickListener listener) { 113 this.confirm_btnText = confirm_btnText; 114 this.confirm_btnClickListener = listener; 115 return this; 116 } 117 118 /** 119 * Set the negative button resource and it's listener 120 * 121 * @param confirm_btnText 122 * @return 123 */ 124 public Builder setNegativeButton(int cancel_btnText, 125 DialogInterface.OnClickListener listener) { 126 this.cancel_btnText = (String) context.getText(cancel_btnText); 127 this.cancel_btnClickListener = listener; 128 return this; 129 } 130 131 /** 132 * Set the negative button and it's listener 133 * 134 * @param confirm_btnText 135 * @return 136 */ 137 public Builder setNegativeButton(String cancel_btnText, 138 DialogInterface.OnClickListener listener) { 139 this.cancel_btnText = cancel_btnText; 140 this.cancel_btnClickListener = listener; 141 return this; 142 } 143 144 /** 145 * Set the netural button resource and it's listener 146 * 147 * @param confirm_btnText 148 * @return 149 */ 150 public Builder setNeutralButton(int neutral_btnText, 151 DialogInterface.OnClickListener listener) { 152 this.neutral_btnText = (String) context.getText(neutral_btnText); 153 this.neutral_btnClickListener = listener; 154 return this; 155 } 156 157 /** 158 * Set the netural button and it's listener 159 * 160 * @param confirm_btnText 161 * @return 162 */ 163 public Builder setNeutralButton(String neutral_btnText, 164 DialogInterface.OnClickListener listener) { 165 this.neutral_btnText = neutral_btnText; 166 this.neutral_btnClickListener = listener; 167 return this; 168 } 169 170 public CustomDialog create() { 171 LayoutInflater inflater = (LayoutInflater) context 172 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 173 // instantiate the dialog with the custom Theme 174 final CustomDialog dialog = new CustomDialog(context, 175 R.style.mystyle); 176 View layout = inflater.inflate(R.layout.customdialog, null); 177 dialog.addContentView(layout, new LayoutParams( 178 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 179 // set the dialog title 180 ((TextView) layout.findViewById(R.id.title)).setText(title); 181 ((TextView) layout.findViewById(R.id.title)).getPaint() 182 .setFakeBoldText(true); 183 184 if (title == null || title.trim().length() == 0) { 185 ((TextView) layout.findViewById(R.id.message)) 186 .setGravity(Gravity.CENTER); 187 } 188 189 if (neutral_btnText != null && confirm_btnText != null 190 && cancel_btnText != null) { 191 ((Button) layout.findViewById(R.id.confirm_btn)) 192 .setText(confirm_btnText); 193 if (neutral_btnClickListener != null) { 194 ((Button) layout.findViewById(R.id.neutral_btn)) 195 .setOnClickListener(new View.OnClickListener() { 196 public void onClick(View v) { 197 neutral_btnClickListener.onClick(dialog, 198 DialogInterface.BUTTON_NEUTRAL); 199 } 200 }); 201 } else { 202 ((Button) layout.findViewById(R.id.neutral_btn)) 203 .setOnClickListener(new View.OnClickListener() { 204 205 @Override 206 public void onClick(View v) { 207 dialog.dismiss(); 208 } 209 }); 210 } 211 } else { 212 // if no confirm button or cancle button or neutral just set the 213 // visibility to GONE 214 layout.findViewById(R.id.neutral_btn).setVisibility(View.GONE); 215 layout.findViewById(R.id.single_line).setVisibility(View.GONE); 216 } 217 // set the confirm button 218 if (confirm_btnText != null) { 219 ((Button) layout.findViewById(R.id.confirm_btn)) 220 .setText(confirm_btnText); 221 if (confirm_btnClickListener != null) { 222 ((Button) layout.findViewById(R.id.confirm_btn)) 223 .setOnClickListener(new View.OnClickListener() { 224 public void onClick(View v) { 225 confirm_btnClickListener.onClick(dialog, 226 DialogInterface.BUTTON_POSITIVE); 227 } 228 }); 229 } else { 230 ((Button) layout.findViewById(R.id.confirm_btn)) 231 .setOnClickListener(new View.OnClickListener() { 232 233 @Override 234 public void onClick(View v) { 235 dialog.dismiss(); 236 } 237 }); 238 } 239 } else { 240 // if no confirm button just set the visibility to GONE 241 layout.findViewById(R.id.confirm_btn).setVisibility(View.GONE); 242 layout.findViewById(R.id.second_line).setVisibility(View.GONE); 243 layout.findViewById(R.id.cancel_btn).setBackgroundResource( 244 R.drawable.single_btn_select); 245 } 246 // set the cancel button 247 if (cancel_btnText != null) { 248 ((Button) layout.findViewById(R.id.cancel_btn)) 249 .setText(cancel_btnText); 250 if (cancel_btnClickListener != null) { 251 ((Button) layout.findViewById(R.id.cancel_btn)) 252 .setOnClickListener(new View.OnClickListener() { 253 public void onClick(View v) { 254 cancel_btnClickListener.onClick(dialog, 255 DialogInterface.BUTTON_NEGATIVE); 256 } 257 }); 258 } else { 259 ((Button) layout.findViewById(R.id.cancel_btn)) 260 .setOnClickListener(new View.OnClickListener() { 261 262 @Override 263 public void onClick(View v) { 264 dialog.dismiss(); 265 } 266 }); 267 } 268 } else { 269 // if no cancel button just set the visibility to GONE 270 layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE); 271 layout.findViewById(R.id.second_line).setVisibility(View.GONE); 272 layout.findViewById(R.id.confirm_btn).setBackgroundResource( 273 R.drawable.single_btn_select); 274 } 275 // set the content message 276 if (message != null) { 277 ((TextView) layout.findViewById(R.id.message)).setText(message); 278 } else if (contentView != null) { 279 // if no message set 280 // add the contentView to the dialog body 281 ((LinearLayout) layout.findViewById(R.id.message)) 282 .removeAllViews(); 283 ((LinearLayout) layout.findViewById(R.id.message)).addView( 284 contentView, new LayoutParams( 285 LayoutParams.WRAP_CONTENT, 286 LayoutParams.WRAP_CONTENT)); 287 } 288 dialog.setContentView(layout); 289 return dialog; 290 } 291 292 } 293 }
2.CustomDialog的样式:
1 <style name="mystyle" parent="@android:style/Theme.Dialog"> 2 <item name="android:windowFrame">@null</item> 3 <item name="android:windowIsFloating">true</item> 4 <item name="android:windowIsTranslucent">false</item> 5 <item name="android:windowNoTitle">true</item> 6 <item name="android:windowBackground">@drawable/bg</item> 7 <item name="android:backgroundDimEnabled">true</item> 8 </style>
3.<item name="android:windowBackground">@drawable/bg</item> -> bg.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <gradient 5 android:angle="90" 6 android:endColor="#ffffff" 7 android:startColor="#ffffff" /> 8 9 <solid android:color="#ffffff" /> 10 11 <corners 12 android:bottomLeftRadius="15px" 13 android:bottomRightRadius="15px" 14 android:topLeftRadius="15px" 15 android:topRightRadius="15px" /> 16 17 </shape>
4.在需要的地方调用即可:
1 private void backDialog() { 2 3 4 CustomDialog.Builder ibuilder = new CustomDialog.Builder(CommentDialogActivity.this); 5 ibuilder.setTitle("提示"); 6 ibuilder.setMessage("是否取消评论?"); 7 ibuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 8 9 public void onClick(DialogInterface dialog, int which) { 10 dialog.dismiss(); 11 finish(); 12 overridePendingTransition(0, R.anim.roll_down); 13 } 14 }); 15 ibuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 16 17 public void onClick(DialogInterface dialog, int which) { 18 dialog.cancel(); 19 } 20 }); 21 ibuilder.create().show(); 22 }
效果如图:
问题:
在使用过程中,当Activity设置为取消标题栏时,会出现CustomDialog变形的情况,如图:
真的很莫名其妙,后面逐一排除比对才找出该原因所在,具体原理不知,恳请知道的大牛评论告知,谢谢。
出现如上问题,是因为:
如下设置取消标题栏:
1 <activity 2 android:name=".CommentDialogActivity" 3 android:label="@string/title_activity_comment" 4 android:screenOrientation="portrait" 5 android:theme="@android:style/Theme.NoTitleBar" 6 android:windowSoftInputMode="adjustUnspecified|adjustResize" > 7 </activity>
而解决该问题,需要使用另一种方式来设置取消标题栏:
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 4 setContentView(R.layout.dialog_sendcomments); 5 }
小伙伴们有兴趣可以试一下。
再个,附上例子源码:链接:http://pan.baidu.com/s/1kTzh5Tt 密码:4437