實作類別似toast效果的圓角dialog警告框

來源:互聯網
上載者:User

標籤:自訂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警告框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.