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); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }
大家不要忽视了这个布局,其实他们两个要完美的配合才能实现的
<?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="@color/colorGray"> <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>