How to put admob banner to bottom of the screen using CoordinatorLayout

拥有回忆 提交于 2019-12-05 00:00:51

问题


I use CoordinatorLayout to make sliding tabs in activity_main.xml file. I want to put admob banner to bottom of the screen. But I have a problem, When I try to code below(activity_main.xml) Admob banner is shown of the top of the screen not bottom of the screen. In fact, I think this admob is shown bottom of the Toolbar(Seen in the picture). But I want to show bottom of the whole screen

       <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:ads="http://schemas.android.com/tools"
        >

  <-- Admob banner:
-->
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_home_footer">

        </com.google.android.gms.ads.AdView>

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="fixed"
                app:tabGravity="fill"/>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"

            />

    </android.support.design.widget.CoordinatorLayout>


回答1:


try below mechanism.Put your viewpager and AdView inside RelativeLayout.

here is the sample layout file.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?actionBarSize">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager"
       >

    </android.support.v4.view.ViewPager>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

    </com.google.android.gms.ads.AdView>
</RelativeLayout>




回答2:


I used @patel's answer but it makes my Tablayout to overlap with the Viewpager's content; The fix I used is to just put the adview below the ViewPager and add

android:layout_gravity="bottom|center"  

Working Code:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_marginBottom="@dimen/dp_56"/>

<com.google.android.gms.ads.AdView
    android:id="@+id/banner_ad_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_56"
    android:visibility="gone"
    ads:adSize="BANNER"
    app:adUnitId="@string/ad_banner_size_id"
    android:layout_gravity="bottom|center"/>

Result:




回答3:


<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom|end"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />



回答4:


    <com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">

</com.google.android.gms.ads.AdView>

//try this comment



来源:https://stackoverflow.com/questions/39539699/how-to-put-admob-banner-to-bottom-of-the-screen-using-coordinatorlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!