【问题标题】:Admob under Floating Action Button浮动操作按钮下的 Admob
【发布时间】:2016-03-03 13:57:13
【问题描述】:

我对浮动操作按钮的位置有疑问。当我从 Admob 工厂投放广告时,它就结束了。我想将广告放在 fab 按钮下,但我尝试的任何方法都不起作用。

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/片段:

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

【问题讨论】:

标签: android android-layout admob floating-action-button


【解决方案1】:

我建议您从 Android Studio 的默认布局中创建包含 fab 按钮的布局。

创建后有2个布局:

  1. contain_main.xml

  2. activity_main.xml

像这样编辑activity_main.xml(我放了一些LinearLayout以便能够设置orientation是垂直的):

activity_main.xml的代码:

<?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>

我试过了,效果很好。

【讨论】:

  • 您应该将 fab 与横幅分开,以避免意外点击和用户沮丧。如果您需要工厂,请将横幅放在屏幕顶部
【解决方案2】:

不要使用FrameLayout 作为广告版式的父级,而是使用垂直LinearLayout。 FrameLayout 将其子项添加为框架。如果您想使用它,请在content_main 中添加一个底部边距,该边距应等于或大于广告布局的高度。

【讨论】:

    【解决方案3】:

    FrameLayout does it like so:

    子视图在堆栈中绘制,最近添加的子视图在顶部。

    因此,如果您希望它出现在其上方,则需要在 AdView 之后/下方添加晶圆厂。

    【讨论】:

      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 2015-12-13
      • 2018-10-18
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多