Android Admob Not enough space to show ad

2019-03-01 02:43发布

问题:

Used the code provided in admob site

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_img"
    android:orientation="vertical"
    android:id="@+id/atozlayout"
     >
<GridView 
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="55dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    
    android:horizontalSpacing="10dp"
   android:verticalSpacing="10dp"  

    >

</GridView>
</LinearLayout>

But the logcat shows E/Ads(4244): Not enough space to show ad! Wants: <480, 75>, Has: <800, 0>

Seems like the layout is creating issue. Please suggest way to fix. Many Thanks.

回答1:

Your grid is taking up all the space with layout_height="fill_parent".

You can use layout_weight="1" and layout_height="0dp" to tell the grid to take up all the remaining space after other views are shown.

Also are you adding the AdView programmatically? You can place it in the xml below the GridView.

Ex:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_img"
    android:orientation="vertical"
    android:id="@+id/atozlayout"
     >
<GridView 
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:columnWidth="55dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:horizontalSpacing="10dp"
   android:verticalSpacing="10dp">

</GridView>
<com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>

</LinearLayout>

Make sure you replace adUnitId with your value.



回答2:

Your gridView is greedy. Its eats all available space on the screen. The solution is to switch the top level layout to a Relative layout, and add android:layout_above"@+id/adview" to the GridView and android:layout_alignParentBottom=true to the ad view. This will force the gridview to leave room beneath it for the ad.



回答3:

According to the Android tab on the page which you linked, you need a <com.google.ads.AdView> view in your layout which seems to be missing from your original post.