android实现类似toast效果的圆角dialog警告框

转自:http://blog.csdn.net/u011007829/article/details/47293597


在最近的项目中需要用到一个类似于toast效果的警告框,而且还要是圆角的。下面是我实现的效果截图:

首先定义一个dialog:

[java]  view plain copy
  1. package com.bobge.doura.customview;  
  2.   
  3. import android.app.Dialog;  
  4. import android.content.Context;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.TextView;  
  9. import com.bobge.doura.R;  
  10.   
  11. /** 
  12.  * Created by Administrator on 2015/6/23. 
  13.  */  
  14. public class CustomDialog extends Dialog {  
  15.     public CustomDialog(Context context, int theme) {  
  16.         super(context, theme);  
  17.     }  
  18.   
  19.     public CustomDialog(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     /** 
  24.      * Helper class for creating a custom dialog 
  25.      */  
  26.     public static class Builder {  
  27.   
  28.         private Context context;  
  29.         private String message;  
  30.         private View contentView;  
  31.   
  32.         public Builder(Context context) {  
  33.             this.context = context;  
  34.         }  
  35.   
  36.         /** 
  37.          * Set the Dialog message from String 
  38.          * 
  39.          * @return 
  40.          */  
  41.         public Builder setMessage(String message) {  
  42.             this.message = message;  
  43.             return this;  
  44.         }  
  45.   
  46.         /** 
  47.          * Set the Dialog message from resource 
  48.          * 
  49.          * @return 
  50.          */  
  51.         public Builder setMessage(int message) {  
  52.             this.message = (String) context.getText(message);  
  53.             return this;  
  54.         }  
  55.   
  56.   
  57.         /** 
  58.          * Set a custom content view for the Dialog. 
  59.          * If a message is set, the contentView is not 
  60.          * added to the Dialog... 
  61.          * 
  62.          * @param v 
  63.          * @return 
  64.          */  
  65.         public Builder setContentView(View v) {  
  66.             this.contentView = v;  
  67.             return this;  
  68.         }  
  69.   
  70.         /** 
  71.          * Create the custom dialog 
  72.          */  
  73.         public CustomDialog create() {  
  74.             LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  75.             // instantiate the dialog with the custom Theme  
  76.             final CustomDialog dialog = new CustomDialog(context, R.style.dialog);  
  77.             View layout = inflater.inflate(R.layout.warm_dialog, null);  
  78.             dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));  
  79.             // set the content message  
  80.             if (message != null) {  
  81.                 ((TextView) layout.findViewById(R.id.tv_warmdialog)).setText(message);  
  82.             }  
  83.             dialog.setContentView(layout);  
  84.             return dialog;  
  85.         }  
  86.     }  
  87. }  

写了一个工具类来显示该dialog:

[java]  view plain copy
  1. package com.bobge.doura.helpr;  
  2.   
  3. import android.content.Context;  
  4. import android.os.Handler;  
  5. import com.bobge.doura.customview.CustomDialog;  
  6.   
  7. /** 
  8.  * Created by Administrator on 2015/6/18. 
  9.  */  
  10. public class ToastShow {  
  11.   
  12.     public static void showCustomDialog(String warmInfo, Context context) {  
  13.         CustomDialog.Builder customBuilder = new  
  14.                 CustomDialog.Builder(context);  
  15.         customBuilder.setMessage(warmInfo);  
  16.         final CustomDialog customDialog = customBuilder.create();  
  17.         customDialog.show();  
  18.         Handler handler = new Handler();  
  19.         handler.postDelayed(new Runnable() {  
  20.             public void run() {  
  21.                 customDialog.dismiss();  
  22.             }  
  23.         }, 1000);  
  24.     }  
  25.   
  26. }  

最重要的一点是要给dialog设置一个style:

[html]  view plain copy
  1. <style name="dialog" parent="@android:style/Theme.Dialog">  
  2.        <item name="android:windowFrame">@null</item>  
  3.        <item name="android:windowIsFloating">true</item>  
  4.        <item name="android:windowIsTranslucent">true</item>  
  5.        <item name="android:windowNoTitle">true</item>  
  6.        <item name="android:background">@drawable/shape_dialog</item>  
  7.        <item name="android:windowBackground">@drawable/shape_dialog</item>  
  8.        <item name="android:backgroundDimEnabled">true</item>  
  9.        <item name="android:backgroundDimAmount">0</item>  
  10.   
  11.    </style>  

[html]  view plain copy
  1. <item name="android:backgroundDimAmount">0</item>  
这一句是设置背景的灰度,这里设置的是0,表示背景灰度完全透明!如此可以实现上面说的效果。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值