Admob 横幅未显示

Admob banner not shown

我在将 adMob 横幅添加到我的应用程序时遇到了一个奇怪的问题。 这是我第一次使用 android studio 执行此操作,所以我遵循了教程(唯一不同的是 build.gradle 文件的编辑)。

我一如既往地在布局中添加了横幅:

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_cut"
    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">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/banner" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/banner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_id" />

</RelativeLayout>

并将横幅初始化到我的 activity:

 AdView mAdView = (AdView) findViewById(R.id.banner);
 AdRequest adRequest = new AdRequest.Builder().build();
 mAdView.loadAd(adRequest);

现在,当我启动我的应用程序时,横幅 space 是空的,并且我收到一些警告,例如

W/Ads﹕ Not enough space to show ad. Needs 360x50 dp, but only has 328x479 dp.

但日志告诉我横幅已正确加载:

I/Ads﹕ Scheduling ad refresh 40000 milliseconds from now.
I/Ads﹕ Ad finished loading.

我该如何解决这个问题?

你会看到,成功了:

I/Ads﹕ Scheduling ad refresh 40000 milliseconds from now.
I/Ads﹕ Ad finished loading.

AdMob 可能会也可能不会抱怨 "having enough room",但如果父视图中有填充,它就不会显示。您需要将想要填充的视图包装在另一个布局中,或者直接向该特定视图添加填充。

请从父布局中移除内边距:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_cut"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/banner" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/banner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_id" />

</RelativeLayout>

如果你还在苦苦挣扎,我已经在 http://www.ahotbrew.com/android-studio-admob-banner-and-interstitial-tutorial/

上找到了一个有效的例子

只需点击下载代码。