2016-03-03 146 views
2

浮動動作按鈕的位置有問題。當我從Admob工廠投放廣告已經結束了。我想將廣告放在fab按鈕下方,但是我嘗試的任何方法都無效。浮動動作按鈕下的Admob

Image of current postion fab to ad

@佈局/ 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> 

@佈局/ 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> 

@佈局/片段:

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

就拿我試過這個方法一看http://stackoverflow.com/questions/30686225/adview-and-floatingactionbutton-overlap-while-using-coordinatorlayout –

+0

,但它不工作。 – szaman

回答

4

我建議您創建包含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> 

我已經嘗試過了,它工作得很好。

enter image description here

+0

不客氣,朋友。 –

+1

您應該將晶圓廠與橫幅分開以避免意外點擊和用戶沮喪。如果你需要一個晶圓廠,把橫幅放在屏幕的頂部 –

0

而不是使用FrameLayout作爲廣告佈局的父級,請使用垂直LinearLayout。 FrameLayout添加它的孩子作爲框架。如果您想使用它,請爲content_main添加底部邊距,該邊距應等於或大於廣告版面的高度。