自定义 Dialog

作者: that_is_this | 来源:发表于2018-04-08 18:46 被阅读31次

1. 如图所示

图片.png

2. 代码如下

    1. my_dialog.xml
<?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:background="#11ffffff">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@color/slid_background"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/titleeee"
            android:layout_width="1dp"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:layout_margin="15dp"
            android:gravity="center"
            android:text="消息提示"
            android:textColor="#38ADFF"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/messageeee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:textColor="@color/title"
            android:textSize="33dp"
            android:textStyle="bold"
            android:layout_gravity="center_horizontal"
            android:text="@string/cardsuccess" />

        <TextView
            android:layout_marginTop="22dp"
            android:id="@+id/timeeee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:textColor="@color/time_show"
            android:textSize="15dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/cardsuccess" />


        <LinearLayout
            android:layout_marginTop="24dp"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/noooo"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:textColor="#7D7D7D"
                android:textSize="16sp" />

            <Button
                android:id="@+id/yessss"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="15dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="right"
                android:paddingRight="10dp"
                android:singleLine="true"
                android:text="@string/ensure"
                android:textColor="@color/time_show"
                android:textSize="17dp" />

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

    1. styles.xml
    <!--自定义dialog背景全透明无边框theme -->
    <style name="MyDialog" parent="android:style/Theme.Dialog">
        <!--背景颜色及和透明程度-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--是否去除标题 -->
        <item name="android:windowNoTitle">true</item>
        <!--是否去除边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--是否模糊-->
        <item name="android:backgroundDimEnabled">false</item>
    </style>

    1. SelfDialog.java

public class SelfDialog extends Dialog {
    private Button yes;//确定按钮
    private Button no;//取消按钮
    private TextView titleTv;//消息标题文本
    private TextView messageTv;//消息提示文本
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;
    private TextView timeShow;
    private String timeStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public SelfDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后,向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });
        //设置取消按钮被点击后,向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            messageTv.setText(messageStr);
        }
        //如果设置按钮的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }
        if (noStr != null) {
            no.setText(noStr);
        }
        if (timeShow != null) {
            timeShow.setText(timeStr);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yessss);
        no = (Button) findViewById(R.id.noooo);
        titleTv = (TextView) findViewById(R.id.titleeee);
        messageTv = (TextView) findViewById(R.id.messageeee);
        timeShow = (TextView) findViewById(R.id.timeeee);
        Log.i("Wooo", " : " + yes + " : " + no + " title :" + titleTv + " ;; " + messageTv);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    public void setTime(String title) {
        timeStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}

3. 调用方式

    private void showDialog(boolean suc) {
        final SelfDialog selfDialog = new SelfDialog(mActivity);
        if (suc) {
            selfDialog.setMessage("打卡成功");
        } else {
            selfDialog.setMessage("打卡失败");
        }

        selfDialog.setTime("打卡时间:" + dialogTime);
        selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                selfDialog.dismiss();
            }
        });
        selfDialog.show();
    }

4. 其他知识点

    1. 添加白线
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginTop="15dp"
            android:background="#E4E4E4" />
            
        <View
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#E4E4E4" />
    1. 设置 png 图片的两个网址

一键生成圆角: http://www.atool.org/roundcorner.php
批量剪裁各种尺寸图标 : http://www.atool.org/ios_logo.php

    1. 几个属性
      android:layout_gravity="center_vertical" 居中
      android:layout_marginTop="22dp" 与上一个试图相隔 22 dp
      android:orientation="vertical" 竖的
      android:orientation="horizontal" 横的

相关文章

  • Dialog

    安卓dialog的使用+如何自定义dialog自定义Dialog自定义Dialog 自定义

  • 自定义Dialog

    自定义Dialog的主题 自定义Dialog的布局文件 继承Dialog 并在onCreate方法中将布局设置给D...

  • 实现图片Dialog中带ViewPager

    效果图 实现思路 自定义Dialog,为Dialog添加自定义布局,自定义PagerAdapter以及PageTr...

  • 【Android】自定义全屏dialog

    一、在themes.xml中添加自定义dialog的样式 二、创建dialog基类 三、创建自定义dialog的布...

  • Android圆角对话框Dialog

    需求:模仿iOS样式Dialog对话框。 自定义Dialog 核心代码: Dialog样式: Dialog布局文件...

  • Android自定义Dialog及其点击事件

    在项目开发中,经常要用到dialog。但是系统的dialog太丑,所有我们要自定义dialog。下面的先介绍自定义...

  • 一个漂亮的自定义Dialog

    这是一个自定义的dialog项目 自定义的dialog,具有如下特点 圆角的dialog View 圆形图片的ti...

  • Flutter Dialog 动画

    本文对 Dialog 做一次系统性学习记录,包括系统 Dialog,自定义 Dialog,Dialog 动画。 A...

  • 自定义Dialog

    仿IOS自定义的Dialog: 1、Util帮助类创建dialog 2、布局文件 :loading_dialog....

  • 自定义Dialog实现透明无遮罩进度框

    效果图: 自定义Dialog继承自Dialog params.dimAmount=0:设置dialog弹出后,背景...

网友评论

    本文标题:自定义 Dialog

    本文链接:https://www.haomeiwen.com/subject/ftumhftx.html