android 比较简单的弹出对话框pupopwindow

呃,其实这个功能很简单,但是,有的时候,可能有的小伙伴需要这种效果却苦苦不知道怎么下手,这里,详细记录下,并且,下面给出下载链接地址,供需要的小伙伴下载使用,首先看下具体实现效果吧,是你需要的可以继续往下看

嗯,话不多说了,下面,请看具体怎么实现吧,

1、首先第一步,我们需要资写布局文件pop_cancle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#50000000"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="274dp"
        android:layout_height="120dp"
        android:background="@mipmap/cancle_pop_bg"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:gravity="center"
            android:text="退出导航后将会返回到首页"
            android:textColor="#3c3c3c"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.1dp"
            android:background="#e8e8e8" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_back_home"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="返回首页"
                android:textColor="#ED4D00" />

            <View
                android:layout_width="0.1dp"
                android:layout_height="match_parent"
                android:background="#e8e8e8" />

            <TextView
                android:id="@+id/tv_cancle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="取消"
                android:textColor="#ED4D00" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

里面我尽量将颜色直接写入xml了,有一个资源文件,弹框背景,这里也一并上传,下面就是

呃,因为是白色背景,有点看不清,点击可以看出来是一个图片

2、然后,我们在MainActivity.java里面加入点击效果,弹出对话框,方法具体如下

      先定义全局变量

    private PopupWindow canclePop;

       然后方法:

    /**
     * 点击弹框
     *
     * @param view
     */
    public void showPop() {
        View contentView = null;
        //设置contentView
        if (canclePop == null) {
            contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_cancle, null);
            canclePop = new PopupWindow(contentView, RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT, true);
            canclePop.setFocusable(true);
            canclePop.setOutsideTouchable(false);
            canclePop.setClippingEnabled(false);
        } else {
            contentView = canclePop.getContentView();
        }
        TextView tvBackHome = contentView.findViewById(R.id.tv_back_home);
        TextView tvCancle = contentView.findViewById(R.id.tv_cancle);

        tvBackHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击返回的逻辑
                canclePop.dismiss();
            }
        });
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                canclePop.dismiss();
            }
        });
        //显示PopupWindow
        View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
        canclePop.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
    }

至此,这个简单的Pupopwindow就实现了,具体点击的效果,要怎么执行,根据你们需求,这里不作更多解释,如果还看不懂的,下面,我给出资源下载链接,有需求的可以下载,呃,这个着实有点抱歉,真心不知道为什么,我上传资源的时候,没有更改下载积分的地方,我都没有设置积分下载,结果,愣是默认给我设置了5积分,更改都没地方更改,希望各位见谅,不过,我相信,上面介绍的地方,一定能看懂,就不需要下面的资源了,如果你积分多的话,想看就下载下吧^_^

资源下载Demo

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值