How to place AdView below the RecyclerView? Please see details

后端 未结 4 732
春和景丽
春和景丽 2021-01-17 02:33

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

What I want is I want to show t

相关标签:
4条回答
  • 2021-01-17 02:54

    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.

    0 讨论(0)
  • 2021-01-17 03:00

    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>
    
    0 讨论(0)
  • 2021-01-17 03:09
    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>
    
    0 讨论(0)
  • 2021-01-17 03:15

    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"/>
    
    0 讨论(0)
提交回复
热议问题