Admob under Floating Action Button

随声附和 提交于 2021-01-27 09:12:27

问题


I have problem with position of floating action button. When i put ad from Admob fab is over it. I want to put ad under fab button but any method that I try does not work.

Image of current postion fab to ad

@layout/activity_layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".activities.MainActivity">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/LzTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/LzTheme.PopupOverlay"/>

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

        <FrameLayout
            android:id="@+id/drawer_layout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include layout="@layout/content_main"/>

            <com.google.android.gms.ads.AdView
                android:id="@+id/ad_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|bottom"
                app:adSize="BANNER"
                app:adUnitId="@string/banner_ad_unit_id"/>
        </FrameLayout>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

@layout/content_main:

<RelativeLayout
    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="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.MainActivity"
    tools:showIn="@layout/activity_main">

    <FrameLayout
        android:id="@+id/FragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

@layout/fragment:

<FrameLayout 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"
             tools:context="com.dsxx.test.fragments.testFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/Recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_dodaj"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"

        app:srcCompat="@drawable/fab_plus"
        app:fabSize="normal"
        app:borderWidth="3dp"
        app:elevation="6dp"/>

</FrameLayout>

回答1:


I suggest you to create layouts that contain the fab button from Android Studio's default layout.

After creating it, there are 2 layouts:

  1. contain_main.xml

  2. activity_main.xml

Edit activity_main.xml (I put some LinearLayout in order to able to set the orientation is vertical) like this :

activity_main.xml's code :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.appschef.mobilequeue.floatingboat.MainActivity">



    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

    <!-- start from here-->
    <!-- LinearLayout for give space fab and AdView-->
    <LinearLayout
    android:orientation="vertical"
    android:gravity="bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="wrap_content">
                <!-- linearLayour for fab button-->
                <LinearLayout
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="right">
                    <android.support.design.widget.FloatingActionButton
                        android:id="@+id/fab"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom|end|left"
                        android:layout_margin="@dimen/fab_margin"
                        android:src="@android:drawable/ic_dialog_email" />
                </LinearLayout>

                <!-- LinearLayour for AdView-->
                <LinearLayout
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <com.google.android.gms.ads.AdView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                    </com.google.android.gms.ads.AdView>
                </LinearLayout>

            </LinearLayout>
    </LinearLayout>
    <!-- end from here-->

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

I have tried it and it does work well.




回答2:


Instead of using a FrameLayout as the parent to your Ad layout, use a vertical LinearLayout. FrameLayout add it's children as frames. If you want to use it, then add a bottom margin to the content_main which should be equal or greater than the height of the Ad layout.




回答3:


The FrameLayout does it like so:

Child views are drawn in a stack, with the most recently added child on top.

So you need to add the fab after/below the AdView, if you want it to appear over it.



来源:https://stackoverflow.com/questions/35774178/admob-under-floating-action-button

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