如果沒有廣告,如何隱藏廣告出現的位置?這是我的廣告。它可以作業并且沒有問題,但是如果沒有廣告,則模板仍然存在。我希望它消失并僅在有廣告時出現
在此處輸入影像描述
原生廣告視圖
AdLoader adLoader = new AdLoader.Builder(this, getString(R.string.admob_native_id4))
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();
TemplateView template = findViewById(R.id.native_ad_1);
template.setStyles(styles);
template.setNativeAd(nativeAd);
}
})
.build();
adLoader.loadAd(new AdRequest.Builder().build());
模板
<com.google.android.ads.nativetemplates.TemplateView
android:id="@ id/native_ad_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:gnt_template_type="@layout/gnt_medium_template_view"
android:visibility="gone" //When I add this, the template disappears, but the ad does not appear
/>
uj5u.com熱心網友回復:
從代碼中控制可見性,試試這個:
AdLoader adLoader = new AdLoader.Builder(this, getString(R.string.admob_native_id4))
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();
TemplateView template = findViewById(R.id.native_ad_1);
template.setStyles(styles);
//// if the ad is not null make the template visible otherwise hide it
if (nativeAd != null){
template.setNativeAd(nativeAd);
template.setVisibility(View.VISIBLE);
}else{
template.setVisibility(View.GONE);
}
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/472082.html
