码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-05 12:55:39      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:自定义dialog   类似toast效果   自定义警告框   style设置背景透明   android   

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

技术分享技术分享

首先定义一个dialog:

package com.bobge.doura.customview;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bobge.doura.R;

/**
 * Created by Administrator on 2015/6/23.
 */
public class CustomDialog extends Dialog {
    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public CustomDialog(Context context) {
        super(context);
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {

        private Context context;
        private String message;
        private View contentView;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Set the Dialog message from String
         *
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }


        /**
         * Set a custom content view for the Dialog.
         * If a message is set, the contentView is not
         * added to the Dialog...
         *
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.warm_dialog, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.tv_warmdialog)).setText(message);
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}

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

package com.bobge.doura.helpr;

import android.content.Context;
import android.os.Handler;
import com.bobge.doura.customview.CustomDialog;

/**
 * Created by Administrator on 2015/6/18.
 */
public class ToastShow {

    public static void showCustomDialog(String warmInfo, Context context) {
        CustomDialog.Builder customBuilder = new
                CustomDialog.Builder(context);
        customBuilder.setMessage(warmInfo);
        final CustomDialog customDialog = customBuilder.create();
        customDialog.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                customDialog.dismiss();
            }
        }, 1000);
    }

}

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

 <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@drawable/shape_dialog</item>
        <item name="android:windowBackground">@drawable/shape_dialog</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0</item>

    </style>

  <item name="android:backgroundDimAmount">0</item>
这一句是设置背景的灰度,这里设置的是0,表示背景灰度完全透明!如此可以实现上面说的效果。

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:自定义dialog   类似toast效果   自定义警告框   style设置背景透明   android   

原文地址:http://blog.csdn.net/u011007829/article/details/47293597

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!