关闭

Android自定义对话框

标签: 对话框
188人阅读 评论(0) 收藏 举报
分类:

对话框自定义类

package com.yunyou.icloudinn.bookhouse.Ui;

/**
 * Created by chen on 2017/10/13.
 */

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yunyou.icloudinn.bookhouse.R;


public class CustomDialog extends Dialog {

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

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private CustomDialog dialog;
        private View layout;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // instantiate the dialog with the custom Theme
            dialog = new CustomDialog(context,R.style.Dialog);
            layout = inflater.inflate(R.layout.dialog_normal_layout, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            // 全屏
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setAttributes(lp);
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

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

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

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }


        public CustomDialog create() {

            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);

            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
//            if (negativeButtonText != null) {
//                ((Button) layout.findViewById(R.id.negativeButton))
//                        .setText(negativeButtonText);
//                if (negativeButtonClickListener != null) {
//                    ((Button) layout.findViewById(R.id.negativeButton))
//                            .setOnClickListener(new View.OnClickListener() {
//                                public void onClick(View v) {
//                                    negativeButtonClickListener.onClick(dialog,
//                                            DialogInterface.BUTTON_NEGATIVE);
//                                }
//                            });
//                }
//            } else {
//                // if no confirm button just set the visibility to GONE
//                layout.findViewById(R.id.negativeButton).setVisibility(
//                        View.GONE);
//            }
            // set the content message
//            if (message != null) {
//                ((TextView) layout.findViewById(R.id.message)).setText(message);
//            } else if (contentView != null) {
//                // if no message set
//                // add the contentView to the dialog body
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .removeAllViews();
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
//            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}


对话框自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:background="@color/grey3">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:gravity="center"
            android:text="图片浏览"
            android:visibility="visible" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >


            <TextView
                android:id="@+id/message"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="left|center"
                android:lineSpacingMultiplier="1.5"
                android:minHeight="120.0dip"
                android:paddingBottom="15.0dip"
                android:paddingLeft="20.0dip"
                android:paddingRight="20.0dip"
                android:paddingTop="15.0dip" />
        </LinearLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1.0px"
            android:background="#ffd0d0d0" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="60.0dip"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/positiveButton"
            android:layout_width="114.0dip"
            android:layout_height="40.0dip"
            android:gravity="center"
            android:text="上一张" />

        <Button
            android:id="@+id/negativeButton"
            android:layout_width="114.0dip"
            android:layout_height="40.0dip"
            android:layout_marginLeft="20.0dip"
            android:gravity="center"
            android:text="下一张" />
    </LinearLayout>

</RelativeLayout>

对话框样式

 
    <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:background">#00000000</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

对话框调用

  if(builder==null)builder = new CustomDialog.Builder(context);
                builder.setMessage("浏览图片");
                builder.setTitle("心情动态");
                builder.setPositiveButton("上一张", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项
                    }
                });

                builder.setNegativeButton("下一张",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                builder.create().show();


0
0
查看评论

Android开发之自定义对话框

由于系统自带的对话框不好看,于是本人就自定义了一个对话框,以后有类似的就可以直接使用啦。先上效果图: 1,布局文件dialog_clear_normal.xml<?xml version="1.0" encoding="utf-8"?> <...
  • duoduo_11011
  • duoduo_11011
  • 2017-01-18 20:54
  • 2007

Android自定义底部显示对话框

1、定义style name="myDialog" parent="android:style/Theme.Dialog"> name="android:windowFrame">@null 边框 --&g...
  • nalw2012
  • nalw2012
  • 2016-01-26 14:15
  • 819

Android 自定义对话框样式

<!-- 设置弹出窗口的属性,frame叠加,isfloat是否浮动,tarnslucent是否半透明,dim是背景是否变暗 --> <style name="Theme_audioDialog" parent="@android:style/...
  • lovoo
  • lovoo
  • 2016-06-14 22:22
  • 390

自定义布局对话框的使用(Dialog)

1、对话框在平时比较常用,实现起来也比较简单。下面是自定义布局对话框的代码: public void showDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inf...
  • u011324501
  • u011324501
  • 2016-06-02 11:30
  • 10225

[Android] Android自定义对话框(Dialog)位置,大小

文章是搜出来的,原文出处:http://www.cnblogs.com/angeldevil/archive/2012/03/31/2426242.html 本文的重点在于自定义Dialog位置时,WindowManager.LayoutParams的x/y值是与Gravity关联的相对值。并在原...
  • sodino
  • sodino
  • 2012-09-03 16:45
  • 6944

android 创建一个简单的自定义对话框

AlertDialog.Builder customizeDialog = new AlertDialog.Builder(context); final View dialogView = LayoutInflater.from(context).inflate(R.layout....
  • qq_24531461
  • qq_24531461
  • 2017-04-14 16:25
  • 394

Android dialog 对话框自定义布局

搞Android四年了 自定义dialog布局一直都写不好。。 今天来汇总一下 Activity中用到自定义的dialog  一般情况下 public class BaseDialog extends Dialog { public BaseDialog(Conte...
  • ID19870510
  • ID19870510
  • 2015-12-25 19:06
  • 3158

Android开发中如何创建自定义对话框

背景: 无论是Android,还是其他什么平台,对话框的主要形式都是弹出,或者我们可以说是一个在当前活动中打开的提示,来执行一些操作或实现一些功能。 你可以用一个对话框来问用户以确认一个行动,向用户通告一个事件,或者提示用户额外信息。因为对话框扰乱UI流,你应该只是把它们当做最后的手段来用。在大...
  • bighead1026
  • bighead1026
  • 2013-09-30 09:27
  • 551

Android实现自定义圆角对话框Dialog

前言:   项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框。         对话框包括:1、圆角         2、app图标 , 提示文本,关闭对话框的"确定"按钮   难点:1、对话框边框圆角显示      2、考虑到提...
  • wjr1949
  • wjr1949
  • 2017-06-09 15:51
  • 302

安卓自定义对话框(位置和大小)

安卓自定义对话框(位置和大小) //自定义对话框 private void showCustomDialog(String msgStr){ try { //使用AlterDialog LinearLayout view = (LinearLayout) La...
  • LiuyiFirstTime
  • LiuyiFirstTime
  • 2016-08-31 14:21
  • 1212
    个人资料
    • 访问:327806次
    • 积分:5473
    • 等级:
    • 排名:第5832名
    • 原创:257篇
    • 转载:11篇
    • 译文:0篇
    • 评论:55条
    微信公众号
    微信公众号
    技术博客交流群
    博客技术交流群
    想要源码加群,群文件里下载
    私信不能及时回复,进群@群主
    个人中心
    博客专栏
    文章分类
    最新评论