如果未加载,则隐藏原生AdView

时间:2016-10-20 07:45:10

标签: android native-ads

我想要实现的是隐藏我的广告视图,如果因为互联网或服务器没有广告而未加载。

MainActivity.java

NativeExpressAdView nativeView =
            (NativeExpressAdView) findViewById(R.id.natvie_ad);
    nativeView.loadAd(new AdRequest.Builder().addTestDevice("testDeviceID").build());

布局

<com.google.android.gms.ads.NativeExpressAdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/moreView"
        android:id="@+id/natvie_ad"
        ads:adSize="300x80"
        ads:adUnitId="adUnitID"
        android:layout_centerHorizontal="true"

        />

1 个答案:

答案 0 :(得分:2)

首先将可见性设置为xml

<com.google.android.gms.ads.NativeExpressAdView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/moreView"
    android:id="@+id/natvie_ad"
    ads:adSize="300x80"
    ads:adUnitId="adUnitID"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    />

然后在你的java代码中设置一个像这样的AdListener

NativeExpressAdView nativeView =
        (NativeExpressAdView) findViewById(R.id.natvie_ad);
nativeView.loadAd(new AdRequest.Builder().addTestDevice("testDeviceID").build());

nativeView.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
            adView.setVisibility(View.VISIBLE);
        }

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

    });

我希望得到帮助:)