如果没有互联网连接,来自 Admob 的 Adview 会显示空白框

Adview from Admob shows blank box if no Internet connection

我只是想在我的布局中包含一个 Adview。工作正常,只是如果没有 Internet 连接它会显示空白 space。正如我目前所读到的,我知道 admob 的标准行为是仅在显示广告时才采用 space。否则,例如。没有互联网连接,应该没有布局 space。我列出了我认为可能出错的代码,这就是我遇到这种奇怪行为的原因...

<LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       xmlns:ads="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:background="#FFFFFF"
       android:weightSum="1">

    <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="?attr/colorPrimary"
       android:theme="@style/AppTheme.AppBarOverlay"
       app:popupTheme="@style/AppTheme.PopupOverlay" />

    <include layout="@layout/content_main"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


    <com.google.android.gms.ads.AdView
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      ads:adSize="BANNER"
      ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

然后,在应用中 gradle:

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.google.firebase:firebase-core:9.2.0'
    compile 'com.google.firebase:firebase-ads:9.2.0'
 }
apply plugin: 'com.google.gms.google-services'

并且在模块 gradle 中:

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0'
    classpath 'com.google.gms:google-services:3.0.0'

我觉得我做错了什么,请大家指教。 谢谢!

Add this Code to your class file

AdView adView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            adView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            adView.setVisibility(View.GONE);
        }
    });