2017-02-12 70 views
1

我有一個浮動動作按鈕應該在右下角,但顯然重力不工作。它顯示在左上角。這裏是我的代碼:浮動動作按鈕重力不工作

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/rl" 
      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"> 

<ImageView 
    android:id="@+id/iv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="16dp" 
    android:src="@mipmap/ic_action_send" 
    app:backgroundTint="@color/colorPrimaryDark" 
    app:layout_anchor="@id/iv" 
    app:layout_anchorGravity="bottom|right|end"/> 

</RelativeLayout> 

回答

4

我有一個浮動的操作按鈕,它應該在右下角 角落,但顯然重力不工作

它,除非你換一個CoordinatorLayout圍繞您的內容預期的行爲。例如

<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" 
    android:fitsSystemWindows="true"> 


    <ImageView 
     android:id="@+id/iv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="16dp" 
     android:src="@mipmap/ic_action_send" 
     app:backgroundTint="@color/colorPrimaryDark" 
     app:layout_anchor="@id/iv" 
     app:layout_anchorGravity="bottom|right|end"/> 
</android.support.design.widget.CoordinatorLayout> 

應該這樣做。你可以閱讀更多關於CoordinatorLayouthere

+1

謝謝,這並獲得成功。 –

0

使用android:layout_gravity="bottom|end"代替app:layout_anchorGravity="bottom|right|end"把它放到年底底部(右下)角球

2

使用android:layout_alignParentBottom="true"android:layout_alignRight="@id/iv"作爲FloatingActionButton參數。

0

對於FrameLayout裏,你可以嘗試:

<FrameLayout 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" 
    android:fitsSystemWindows="true" 
    tools:context="ch13mob.wifiofworldairports.fragment.FragmentWifiAirports"> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_wifi" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_wifi_black_24dp" /> 

</FrameLayout>