简体   繁体   中英

Displaying admob banner ad below a listview within a fragment

I am trying to display an Admob banner ad below my listview in fragment A. For some reason it does not show in the same manner as in fragment B, where I place the ad below a set of textviews.

In fragment B, the ad shows up centered at the bottom of the screen, and appears to be in a plane "above" the scrollable set of textViews.

In fragment A, the ad shows up in a definite area at the bottom of the screen, in the same "layer" as the listView. If I do not specify the 50 dp marginBottom attribute, the listview does not show.

Here is my layout xml for Fragment A:

<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:orientation="vertical"
android:background="@color/windowBackground">


<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp">

</ListView>

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


</RelativeLayout>

Here is my layout xml for Fragment B:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/relativelayout"
    >



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:textStyle="normal"

        android:id="@+id/text1"
        android:text="@string/textdesc1"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:textStyle="normal"

        android:id="@+id/text2"
        android:text="@string/textDesc2"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/text1"
        android:layout_marginRight="10dp"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:textStyle="normal"

        android:id="@+id/text3"
        android:text="@string/textDesc3"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/text2"
        android:layout_marginRight="10dp"
        />
</RelativeLayout>
</ScrollView>

<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"
    android:layout_marginBottom="20dp"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>

How to achieve the same effect as what I see in fragment B in fragment A? I don't want the current display in fragment A because, if the ad does not load for some reason, the vacant area at the bottom of the screen will appear ugly. If atleast the fragment B's effect is seen here, then even if the ad does not load, the screen looks intact.

Also, once this is done, what measures can be taken to avoid the overlap of the ad with the last element in the list?

Try putting the ListView above the AdView :

<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:orientation="vertical"
    android:background="@color/windowBackground">

     <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"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/adView"/>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM