Android 每次进入app弹出一个pupuwindow广告对话框

在网上找了大半天,都没有找到合适的例子,终于靠自己的努力,实现了这个功能

给大家分享一下效果图:

//这是一个方法,直接放在onCreat方法里面就行了,我之前放的方法,没有进行耗时操作,就一直报找不到pupuwindow的布局,最后下了个子线程,然后popuwindow居然失效,所以给大家分享一下我的解决办法。

 
private void fistPopuwindow() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1500);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        View view = View.inflate(MainActivity.this, R.layout.first_popuwindow, null);
                        ImageView mImg_first = view.findViewById(R.id.mImg_first);
                        LinearLayout mLine = view.findViewById(R.id.mLine);
                        final PopupWindow mPopu = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT);
                        mPopu.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
                        mLine.getBackground().setAlpha(100);
                        mLine.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mPopu.dismiss();
                            }
                        });
                        mImg_first.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ToastUtils.makeShortToast(MainActivity.this,"我还没有写内容呢");
                                mPopu.dismiss();
                            }
                        });
                        mPopu.setOutsideTouchable(true);//判断在外面点击是否有效
                        mPopu.setFocusable(true);
                        mPopu.showAsDropDown(view);
                        mPopu.isShowing();
                    }
                });


            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();


}

大家不要忽视了这个布局,其实他们两个要完美的配合才能实现的,这个颜色带有,不然的话就会报错,因为在popuwindow里面对这个颜色进行了透明设置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mLine"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#88323232">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/mImg_first"
            android:layout_width="220dp"
            android:layout_height="220dp"
            android:layout_centerInParent="true"
            android:src="@mipmap/ic_launcher"
            />
    </RelativeLayout>
</LinearLayout>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值