如何在RecyclerView下面放置AdView?请看细节

时间:2016-06-22 20:19:20

标签: android android-layout android-recyclerview android-xml adview

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

我想要的是我想在AdView下面显示RecyclerView,为此我已经这样做了:

<?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覆盖整个区域,广告也被它覆盖。

这是截图(Recyclerview已滚动到最后一个,并且没有广告):

enter image description here

请告知我如何将广告放在RecyclerView下方或者Recyclerview结束处。

4 个答案:

答案 0 :(得分:3)

您应该使用LinearLayout代替RelativeLayout。在LinearLayout将广告放在屏幕底部,让它占据空间并将layout_weight设置为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>

还有一件事,layout_height的{​​{1}}设置为AdView,而是使用match_parent

答案 1 :(得分:1)

尝试将您的RecyclerView放入其他布局,或者只设置layout_above =&#34; @ id / card_ads&#34;

我认为主要的问题是你在card_ads layout_below中设置,但不告诉recyclerview它应该在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>

我已在我的应用上添加Admob,您可以在Github

查看

修改

我把匹配父母,看它是否有效

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

答案 2 :(得分:0)

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>

答案 3 :(得分:0)

将其添加到您的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>