Android 每次進入app彈出一個pupuwindow廣告對話方塊
阿新 • • 發佈:2019-01-09
在網上找了大半天,都沒有找到合適的例子,終於靠自己的努力,實現了這個功能
給大家分享一下效果圖:
//這是一個方法,直接放在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(); }
大家不要忽視了這個佈局,其實他們兩個要完美的配合才能實現的
<?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>