Android 自定义dialog方法和原理

自定义dialog

Posted by BY wangchuanwen on May 23, 2018

关于自定义Android dialog的使用方法和自定义dialog方法和详解原理

##资源文件的style,给dialog的设置自己风格的主题,

<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>  

##给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="#ffffff">  
<LinearLayout  
    android:layout_width="260dp"  
android:layout_height="wrap_content"  
android:layout_centerInParent="true"  
    android:background="@drawable/  
    free_dialog_bg"  
    android:orientation="vertical">  
  
    <TextView  
        android:id="@+id/title"  
 android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
        android:layout_gravity="center"  
        android:layout_margin="15dp"  
        android:gravity="center"  
        android:text="消息提示"  
        android:textColor="#38ADFF"  
        android:textSize="16sp" />  
  
    <TextView  
        android:id="@+id/message"  
    android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
    android:layout_marginLeft="20dp"  
    android:layout_marginRight="20dp"  
        android:text="提示消息" />  
  
    <View  
    android:layout_width="match_parent"  
        android:layout_height="1px"  
        android:layout_marginTop="15dp"  
        android:background="#E4E4E4" />  
    <LinearLayout  
    android:layout_width="match_parent"  
        android:layout_height="40dp"  
    android:orientation="horizontal">  
        <Button  
            android:id="@+id/no"  
            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:text="No"  
            android:textColor="#7D7D7D"  
            android:textSize="16sp" />  
  
        <View  
            android:layout_width="1px"  
            android:layout_height="match_parent"  
            android:background="#E4E4E4" />  
  
        <Button  
            android:id="@+id/yes"  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_marginRight="10dp"  
            android:layout_weight="1"  
            android:background="@null"  
            android:gravity="center"  
            android:singleLine="true"  
            android:text="Yes"  
            android:textColor="#38ADFF"  
            android:textSize="16sp" />  
    </LinearLayout>  
</LinearLayout>  

</RelativeLayout>

##drawable的自定dialog背景框,可以自定义命名

##点开链接直接下载,好的同学点个star哈

###说一下原理

1、通过构造方法给dialog设置一个主题 R.style.MyDialog , 主要设置dialog的显示属性,一般都是 全透明无边框

2、然后在dialog的onCreate()方法中,用setContentView( R.layout.SelfDialog) 为dialog设置XML文件,我们就可以在layout文件中创建自定义的Dialog风格。这里我就自定义了xml文件格式,实现了自定义的外观风格,不受系统的主题影响。

##3、然后通过设置要为外界设置一些public 公开的方法,来向自定义的dialog传递值。这里的title 和 message,都是可以通过外界传值进来,进行设置的。如下面的public 方法就是供外界activity来设置title和message的:

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

/** * 从外界Activity为Dialog设置dialog的message * * @param message */

public void setMessage(String message) {
messageStr = message;
}

在activity通过实例化Dialog后就可以设置titile和message了。 selfDialog = new SelfDialog(MainActivity.this);
selfDialog.setTitle(“提示”);
selfDialog.setMessage(“确定退出应用?”);

4、最后,自定义的dialog中包含了一些按钮的时候,这个时候要想让按钮有点击事件,并且把这个点击事件能够传递给activity,让acitvity做一些事情,这里就需要设置监听接口,让button的点击事件能够让外界activity知道。如下面的代码。

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

public interface onNoOnclickListener {
public void onNoClick();
}

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;
}

//设置确定按钮被点击后,向外界提供监听
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();
}
}
});

   ##activity就可以设置监听接口来实时获取button的点击事件如下:

selfDialog.setYesOnclickListener(“确定”, new SelfDialog.onYesOnclickListener() {
@Override
public void onYesClick() {
Toast.makeText(MainActivity.this,”点击了–确定–按钮”,Toast.LENGTH_LONG).show();
selfDialog.dismiss();
}
});
selfDialog.setNoOnclickListener(“取消”, new SelfDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(MainActivity.this,”点击了–取消–按钮”,Toast.LENGTH_LONG).show();
selfDialog.dismiss();
}
});

通过上面4步的详细讲解,就能知道自定义dialog的一般原理,基本就是 要为dialog设置一些基本的文字信息时,就直接用公开方法public 的方法,让外界直接设置;如果要让activity监听到button之类的点击事件就自定义接口,用监听接口的方式向activity传递点击事件。

源码下载https://github.com/gabyallen/Mydialog/