如何将AdView放在RecyclerView下方?请查看详细信息

问题描述:

我正在开发一个应用,其中某些数据是从 Firebase 中加载的,并显示在 RecyclerView 中。

I'm developing an app in which some data is getting loaded from Firebase and is shown in the RecyclerView.

我想要的是我想在 RecyclerView下面显示 AdView code>,为此,我这样做:

What I want is I want to show the AdView below the RecyclerView and for that I have done this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/recyclerView_parentLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <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_below="@id/recycler_view"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentPaddingBottom="2dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true" >

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

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

</RelativeLayout>

问题在于此布局的底部没有广告,recyclerview覆盖了整个区域

The problem is that this layout is showing no ads at the bottom and the recyclerview is covering the whole area and the ads are getting covered by it.

这是屏幕截图(recyclerview已滚动到最后一个,那里没有广告):

Here's the screenshot (the recyclerview has been scrolled to the last and there is no ad there):

请让我知道如何将广告放置在 RecyclerView 或recyclerview结束的地方。

Please let me know how can I place the ads just below the RecyclerView or where the recyclerview ends.

您应使用 LinearLayout 而不是 RelativeLayout 。在 LinearLayout 中,将广告放在屏幕底部,让其占据空间,然后将 layout_weight 设置为您的 RecyclerView

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>

还有一件事,不要设置 AdView > layout_height 到 match_parent ,使用 wrap_content 而是code>。

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