天天看点

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

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

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

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

首先定义一个dialog:

[java] view

plaincopy

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 

        public builder setmessage(int message) {  

            this.message = (string) context.gettext(message);  

         * set a custom content view for the dialog. 

         * if a message is set, the contentview is not 

         * added to the dialog... 

         * @param v 

        public builder setcontentview(view v) {  

            this.contentview = v;  

         * 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.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:

[html] view

<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,表示背景灰度完全透明!如此可以实现上面说的效果。

继续阅读