admob 查看测试广告底部的空白

admob view white gap at bottom on test ads

在我的应用程序中,我使用片段。我在 main activity 中初始化了 adview。每个片段都有一个列表视图,列表视图的底部有一个 admob 视图。测试广告出现在我的第一个片段中,但是当我导航到另一个片段时,虽然我尝试使用 destroy() 当片段的 onPause() 功能被触发时,列表视图底部有一个白色间隙我登陆的新片段!我没有在新片段中创建任何广告视图!有什么想法为什么会这样吗?

感谢任何帮助!

这是我的homefragment.xml

<ListView
    android:id="@+id/homelistview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/adView1"
    app:layout_constraintTop_toTopOf="parent"
    />

<com.google.android.gms.ads.AdView
    android:id="@+id/adView1"
    android:contentDescription="@string/admobviewDescription"
    android:layout_centerHorizontal="true"
    app:adSize="SMART_BANNER"
    app:adUnitId="ca-app-pub-3940256099942544/6300978111"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/homelistview"
    >
</com.google.android.gms.ads.AdView>

更新

我找到了这个页面 https://developers.google.com/admob/android/custom-events 其中提到以下内容。有人知道这是否意味着无法暂停测试广告?

@Override
    public void onPause() {
        // The sample ad network doesn't have an onPause method, so does nothing.
    }

找到了应该怎么做。 Onpause() 可以顺便在测试广告时使用,不知道样本广告网络是什么。所以这是你应该用来消除这个空白的代码。实际上,您必须从父布局中删除您创建的 adview,以便删除视图和白色间隙,然后添加一个新的 adview。我也试过 adview.removeallviews() 但没有用。只有这段代码有效。

// Remove the ad keeping the attributes
        AdView ad = (AdView) findViewById(R.id.adView1);
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) ad.getLayoutParams();
        ConstraintLayout parentLayout = (ConstraintLayout) ad.getParent();
        parentLayout.removeView(ad);

// Re-initialise the ad
        ad.destroy();
        ad = new AdView(this);
        ad.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);

       //test ads
        ad.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        ad.setId(R.id.adView1);
        ad.setLayoutParams(lp);
        parentLayout.addView(ad);

// Re-fetch add and check successful load
        AdRequest adRequest = new AdRequest.Builder().build();
        ad.loadAd(adRequest);