简体   繁体   中英

ListView on TOP of Ads - Android

I'm having troubles showing ads on bottom of a listview, on my main activity the ads works, the only difference there is that I use a viewpager instead of listview.

This is the layout:

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

    <com.google.ads.AdView
        xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:loadAdOnCreate="true"
        android:background="#313131"
        app:adSize="SMART_BANNER"
        app:adUnitId="MY_ID" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/adView" />

</RelativeLayout>

The listview shows on TOP of the ads and of course can't be clicked, but this same layout works with a viewpager.

Another way.

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

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

<com.google.ads.AdView
    xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:loadAdOnCreate="true"
    android:background="#313131"
    app:adSize="SMART_BANNER"
    app:adUnitId="MY_ID" />

</LinearLayout >

When Ad - is loaded, get his height. Then ListView bottomMargin = adHeight and Ad topMargin = (-1)*adHeight . Try it.

Final layout:

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

    <com.google.ads.AdView
        xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:loadAdOnCreate="true"
        android:background="#313131"
        app:adSize="SMART_BANNER"
        app:adUnitId="MY_ID" />

    <LinearLayout
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/adView" />

</RelativeLayout>

And then replaced:

getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

With:

getSupportFragmentManager().beginTransaction().add(R.id.listview, list).commit();

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
   />

<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#313131"
    ads:loadAdOnCreate="true"
    ads="SMART_BANNER"
    ads="MY_ID" />

NB This is a layout question. It has nothing to do with Admob. If you are using a LinearLayout then just set layout_height="wrap+content" and layout_gravity="1" for the item that you want to expand to consume the unused space. In this case your ListView.

Your problem was really the layout_height="match_parent"

NB I also removed the unnecessary renaming of the ads namespace.

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