在linearlayout中显示admob横幅

时间:2015-03-26 20:10:38

标签: android admob android-linearlayout

我试图在列表活动中显示一个admob横幅,但我遇到了问题。我在google开发者网站上使用了admob快速入门教程,该教程使用了relativelayout,而我使用的是linearlayout。我已经尝试根据之前在SO上的答案调整我的主xml,结果各不相同,有时应用会在启动时停止/关闭,有时候listactivity仍为空白。我原来的xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@null" />

<WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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


</LinearLayout>

有了这个,应用程序运行正常,但logcat说没有足够的空间来显示广告,当我将广告视图移到列表视图上方时,应用程序会在启动时停止。我已尝试使用relativelayout进行不同的实现,如下所示,但这会导致应用在启动时关闭

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

<LinearLayout
     android:layout_alignParentTop="true"
    android:layout_above="@id/adView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@null" />

<WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

</RelativeLayout>

要求投放广告

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

广告的Logcat是

03-27 12:24:38.697: W/Ads(2094): Not enough space to show ad. Needs 320x50 dp, but only has 480x0 dp.
03-27 12:24:38.847: I/Ads(2094): Scheduling ad refresh 60000 milliseconds from now.
03-27 12:24:38.887: I/Ads(2094): Ad finished loading.
03-27 12:24:38.887: W/Ads(2094): Not enough space to show ad. Needs 320x50 dp, but only has 480x0 dp.
03-27 12:24:38.897: W/Ads(2094): Not enough space to show ad. Needs 320x50 dp, but only has 480x0 dp.

请帮忙。提前致谢

1 个答案:

答案 0 :(得分:0)

使用layout_weight,尝试以下方法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
     android:layout_weight="1"
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@null" />

<WebView
    android:id="@+id/webview"
     android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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


</LinearLayout>