How to place AdView below the RecyclerView? Please see details

回眸只為那壹抹淺笑 提交于 2019-12-01 14:34:37

You should use LinearLayout instead of RelativeLayout. In LinearLayout put ad at the bottom of the screen and let it occupy it's space and set layout_weight to your RecyclerView.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ... >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ... />

    <android.support.v7.widget.CardView
        android:id="@+id/card_ads"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... >

        <com.google.android.gms.ads.NativeExpressAdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ... />

    </android.support.v7.widget.CardView>

</LinearLayout>

And one more thing, don't set layout_height of AdView to match_parent, use wrap_content instead.

Try to put your RecyclerView inside other layout, or just set layout_above="@id/card_ads"

I think the main problem is that you set in card_ads layout_below, but not tell recyclerview that it should be over the card_ads

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerView_parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.abc.xyz.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="card_ads"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </LinearLayout>

    <android.support.v7.widget.CardView
        android:id="@+id/card_ads"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/recycler_view"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true"
        app:contentPaddingBottom="2dp">

        <com.google.android.gms.ads.NativeExpressAdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true"
            ads:adSize="280x80"
            ads:adUnitId="ca-app-pub-***********/*********">
        </com.google.android.gms.ads.NativeExpressAdView>

    </android.support.v7.widget.CardView>

</RelativeLayout>

I've add Admob on my App, you can check at Github

Edit:

I put match parent, see if it works

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Add framelayout as a main layout .Try this code .Put your adView insted of button .
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerView_parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"


    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.v7.widget.CardView
            android:id="@+id/card_ads"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/recycler_view"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            app:cardElevation="2dp"
            app:cardUseCompatPadding="true"
            app:contentPaddingBottom="2dp">

            <com.google.android.gms.ads.NativeExpressAdView
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_centerInParent="true"
                ads:adSize="280x80"
                ads:adUnitId="ca-app-pub-***********/*********">
            </com.google.android.gms.ads.NativeExpressAdView>

        </android.support.v7.widget.CardView>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>
</FrameLayout>

Add this to your XML

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/adView"
    android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!