How to make AdView “occupy” space even while requesting an ad? (Android)

☆樱花仙子☆ 提交于 2019-12-04 17:51:51

问题


I'm using Google AdMob Ads SDK 4.0.4 for Android

By default, The AdView will have no size until the ad is loaded. Which could cause problem if you have buttons above or below the ad.

User could accidentally click on the ad if the ad returned at the exact moment they are about to click a button.

In old admob SDK I solved this by using setGoneWithoutAd(false). This way, the space will be preserved even when the ad is not returned yet.

In the new SDK (Google Admob Ads SDK 4.0.4) I manage to do the same by using this quick fix: reserve the space by putting ad in some layout that has width="320dp" and height="50dp"

<LinearLayout 
                        android:layout_width="320dp" 
                        android:layout_height="50dp" > 
                  <com.google.ads.AdView android:id="@+id/adview" 
                         android:layout_width="wrap_content" 
                         android:layout_height="wrap_content" 
                         ads:adUnitId="xxxxxxxxxxx" 
                         ads:adSize="BANNER"/> 
</LinearLayout> 

It works but I'm not sure if this is a proper method.(Will I run into the famous "Not enough space to show ad!" issue? )

To sum up the question: How to(properly) make AdView "occupy" space even while requesting an ad?

Thank you in advance!


回答1:


Have you tried:

 <com.google.ads.AdView android:id="@+id/adview" 
                     android:layout_width="320dip" 
                     android:layout_height="50dip" 
                     ads:adUnitId="xxxxxxxxxxx" 
                     ads:adSize="BANNER"/> 

I use AdWhirl to serve my ads for me and the way I have it is with a fixed view similar to this with fixed dimensions. Looks like you have it set to wrap content making the view disappear when nothing's there. Try it out and let me know how it works out.




回答2:


First you have to decide the ad size that you want. There are a couple of pre-defined ones.

The easiest to work with is SMART_BANNER, it will adapt it's width to match the current screen width, and it's height will be 32, 50 or 90 dp for screen heights <=400, (>400 && <=720) or >720 dp respectively.

Once you know the type of banner you will request, you declare it on adSize property and reserve the space for this using layout_width and layout_height properties.

For example for an SMART_BANNER you can do something like this:

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="@dimen/ad_banner_height"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="xxxxxxxxx"/>

And you will set ad_banner_height to the following values:

values/dimens/ad_banner_height --> 32dp
values-h400dp/dimens/ad_banner_height --> 50dp
values-h720dp/dimens/ad_banner_height --> 90dp

For other banner sizes, check: https://developers.google.com/admob/android/banner#banner_sizes



来源:https://stackoverflow.com/questions/5715798/how-to-make-adview-occupy-space-even-while-requesting-an-ad-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!