开发者

ListView taking up all space, when trying to use an Admob Adview in Android

开发者 https://www.devze.com 2023-03-13 19:50 出处:网络
I am trying to get Google Admob advertisements to display on the main screen of an app tha开发者_运维技巧t consists of a ListView. Unfortunately, the ListView is taking up all of the space on the scre

I am trying to get Google Admob advertisements to display on the main screen of an app tha开发者_运维技巧t consists of a ListView. Unfortunately, the ListView is taking up all of the space on the screen, and then the advertisement appears over the top of it. I have tried putting the ListView at the top, followed by an AdView, and I've also tried putting the AdView at the top, followed by the ListView, but nothing works.

If I set the size of the LinearLayout enclosing the ListView to a fixed height (eg, 200px) then it it limits the size and fixes the problem, but I don't want to do this, because screen heights of Android devices vary so much. Is there no way to tell the ListView to not take up all the space without setting a fixed size?

My code is below, I'd be grateful for any help:

<?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>


Both of your LinearLayouts have height of fill_parent. That means that they'll both be the full height of their parent. Clearly not what you want. Try this:

<?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>


You have a relative layout. you need to set the listview property to layout_below="yourAdLayout" or rename the parent layout to linear layout. the orientation flag is not used in a relative layout, only in linear.

0

精彩评论

暂无评论...
验证码 换一张
取 消