我试图让 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>