0

我试图让 Google Admob 广告显示在由 ListView 组成的应用程序的主屏幕上。不幸的是,ListView 占用了屏幕上的所有空间,然后广告出现在它的顶部。我尝试将 ListView 放在顶部,然后是 AdView,我也尝试将 AdView 放在顶部,然后是 ListView,但没有任何效果。

如果我将包含 ListView 的 LinearLayout 的大小设置为固定高度(例如,200px),那么它会限制大小并解决问题,但我不想这样做,因为 Android 设备的屏幕高度差异很大. 有没有办法告诉 ListView 在不设置固定大小的情况下不占用所有空间?

我的代码如下,如果有任何帮助,我将不胜感激:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
      android:layout_alignParentTop="true"
      android:id="@+id/ad_layout"
      android:orientation="vertical"
      android:gravity="top"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

                   <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="AD_UNIT_ID"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="true"/>

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_alignParentBottom="true"
      android:layout_below="@+id/ad_layout"
      android:orientation="vertical"
      android:gravity="bottom"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

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

        <TextView android:id="@+id/android:empty"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/no_hosts"/>

</LinearLayout>

</RelativeLayout>
4

2 回答 2

2

您的两个 LinearLayout 的高度均为fill_parent. 这意味着他们都将成为他们父母的全高。显然不是你想要的。尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <com.google.ads.AdView android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adUnitId="AD_UNIT_ID"
            ads:adSize="BANNER"
            ads:loadAdOnCreate="true"/>
      <ListView android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
      <TextView android:id="@+id/android:empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/no_hosts"/>
</LinearLayout>
于 2011-06-16T08:03:29.617 回答
0

你有一个相对的布局。您需要将 listview 属性设置为 layout_below="yourAdLayout" 或将父布局重命名为线性布局。方向标志不用于相对布局,仅用于线性。

于 2011-06-16T08:00:05.343 回答