Not enough space to show ad (AdMob)

佐手、 提交于 2019-12-17 07:48:19

问题


I am trying to show AdMob ads in my activity, but it is always giving error that "not enough space to show ad".

My XML file is:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.google.ads.AdView
        android:id="@+id/adMobadView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admobid"
        android:layout_alignParentBottom="true"
        android:gravity="bottom|center_horizontal"
        ads:loadAdOnCreate="true" />
</RelativeLayout>

My Java file:

    AdView ad=(AdView)findViewById(R.id.adMobadView);

    AdRequest re = new AdRequest();
    re.setTesting(true);
    re.setGender(AdRequest.Gender.FEMALE);
    ad.loadAd(re);

LogCat:

   02-18 14:16:17.869: W/webcore(813): Can't get the viewWidth after the first layout
   02-18 14:16:17.979: I/Ads(813): onReceiveAd()
   02-18 14:16:17.979: W/Ads(813): Not enough space to show ad! Wants: <320, 50>, Has: <288, 430>

回答1:


The left and right padding of your RelativeLayout is consuming space required by your AdView.

Change RelativeLayout config to :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 ...
 android:paddingLeft="0dp"
 android:paddingRight="0dp"
 ...
>

You probably also want to change your AdView config to:

<com.google.ads.AdView
        android:id="@+id/adMobadView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        />

as you don't really want your AdView to consume the entire screen.




回答2:


Check the dimensions of the screen and the paddings, and see if it actualy fits.

Also, try to change the Adview to this:

    <com.google.ads.AdView
        android:id="@+id/adMobadView"
        android:layout_width="320dp"
        android:layout_height="50dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admobid"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:loadAdOnCreate="true" />



回答3:


to show ads inside relative layout you can follow this design

<RelativeLayout
        android:id="@+id/relativeLayoutpopular"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_above="@+id/bottomBar"
        android:layout_below="@+id/relativeLayoutadd"
        android:layout_marginTop="5dp"
        >
            <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="3dp">
         <com.google.ads.AdView
             xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
             android:id="@+id/adView"
             android:layout_width="fill_parent"
            android:layout_weight="0.9"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            android:background="#2d2f2f"
            ads:adUnitId="admobId" />
        </LinearLayout>
    </RelativeLayout>



回答4:


i had the same problem with NativeExpressAdView. If you add padding or margin to your layout, I think, it's not a good idea to fix the ad's size width. I sugger to do in programmatically

  1. First step : get the dp size of screen witdh

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); //float dpHeight = displayMetrics.heightPixels / displayMetrics.density; dpWidth = displayMetrics.widthPixels / displayMetrics.density;

2. Second step:

mAdView.setAdSize(new AdSize((int) dpWidth, 150));




回答5:


Simply set the margins to 0dp in res->values->dimens.xml

<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>


来源:https://stackoverflow.com/questions/21848532/not-enough-space-to-show-ad-admob

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