Android FlycoDialog 简单实用的自定义Android弹窗对话框之Dialog篇

效果图镇楼 
版本更新页 广告页

FlycoDialog是一款非常棒的弹窗对话框处理框架,今天在这里主要讲一下他的自定义弹出对话框的功能,这里以第二幅效果图为例,图片已经放在博客最下方,X号自己随便找一个东西代替吧。 
首先我们还是先添加依赖。

compile 'com.flyco.dialog:FlycoDialog_Lib:1.3.2@aar'

然后我们先写一个弹窗的布局,非常简单一个大的ImageView展示图片,上面放一个小的ImageView用于点击使弹框消失。

<?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">
    <ImageView
        android:id="@+id/iv_ad"
        android:layout_width="278dp"
        android:layout_height="392dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <ImageView
        android:id="@+id/ad_back"
        android:layout_width="278dp"
        android:layout_height="45dp"
        android:layout_alignRight="@id/iv_ad"
        android:layout_alignTop="@id/iv_ad"
        android:background="#01ffffff"
        />
</RelativeLayout>

然后我们来写弹框的逻辑代码,我会用注释的方式来解释代码的功能。

public class AdDialog extends BaseDialog<AdDialog> {
    private Context context;
    private ImageView iv_ad;
    private ImageView back;

    public AdDialog(Context context) {
        super(context);
        this.context = context;
    }

    //该方法用来出来数据初始化代码
    @Override
    public View onCreateView() {
        widthScale(0.85f);
        //填充弹窗布局
        View inflate = View.inflate(context, R.layout.addialog, null);
        //用来放整个图片的控件
        iv_ad = (ImageView) inflate.findViewById(R.id.iv_ad);
        //放在透明部分和错号上的隐形控件,用来点击使弹窗消失
        back = (ImageView) inflate.findViewById(R.id.ad_back);
        //用来加载网络图片,填充iv_ad控件,注意要添加网络权限,和Picasso的依赖和混淆
        Picasso.with(context)
                .load("https://img-blog.csdn.net/20170906094014301?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzY2MjE5OTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast")
                .into(iv_ad);

        return inflate;
    }
    //该方法用来处理逻辑代码
    @Override
    public void setUiBeforShow() {
        //点击弹窗相应位置,处理相关逻辑。
        iv_ad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"哈哈",Toast.LENGTH_SHORT).show();
                //处理完逻辑关闭弹框的代码
                dismiss();
            }
        });
        //点×关闭弹框的代码
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭弹框的代码
                dismiss();
            }
        });
    }
}

最后来写启动弹框的代码

AdDialog adDialog = new AdDialog(this);
        adDialog.onCreateView();
        adDialog.setUiBeforShow();
        //点击空白区域能不能退出
        adDialog.setCanceledOnTouchOutside(true);
        //按返回键能不能退出
        adDialog.setCancelable(true);
        adDialog.show();

如果需要处理比较复杂的逻辑可通过AdDialog的构造方法像弹窗内传值,例如

弹框逻辑代码的构造方法

 public AdDialog(Context context,String ivurl,String title,String adurl) {
        super(context);
        this.context = context;
        this.ivurl = ivurl;
        this.title = title;
        this.adurl = adurl;
    }

启动代码时传入数据

      AdDialog adDialog = new AdDialog(getActivity(),"图片网址","标题","广告网址");
        adDialog.onCreateView();
        adDialog.setUiBeforShow();
        //点击空白区域能不能退出
        adDialog.setCanceledOnTouchOutside(true);
        //按返回键能不能退出
        adDialog.setCancelable(true);
        adDialog.show();

如果还有什么不明白的地方可在博客下留言。

猜你喜欢

转载自www.cnblogs.com/zhujiabin/p/9768109.html