2015-06-10 7 views
29

Ich möchte nach unten rechts meine FAB ausrichten.Unten ausrichten Floating Action Button

  1. Ich habe versucht, mit android:gravity="bottom|right"
  2. Wenn ich android:layout_alignParentBottom="true versuchen "FAB verschwindet
  3. Wenn ich versuche, android:layout_alignBottom="@id/lista_tiendas"FAB verschwindet

Es ist nicht kompliziert erscheinen, aber ich kann einfach nicht erreichen es

Irgendwelche Ideen?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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"> 

<ListView 
    android:id="@+id/lista_tiendas" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="4.0sp"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_add_white_48dp" 
    app:backgroundTint="@color/spg_rosa" 
    app:borderWidth="0dp" 
    app:elevation="8dp" 
    app:fabSize="normal" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="@id/lista_tiendas" 
    /> 
</RelativeLayout> 

Antwort

77

Für RelativeLayout:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    ... /> 

Für CoordinatorLayout Sie android:layout_gravity="end|bottom"


Für ConstraintLayout verwenden sollten:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    ... /> 

Weitere Informationen finden Sie unter this answer.

+1

Ja, ich habe das Codeformat korrigiert, sodass RelativeLayout erscheint! Tx –

+0

Das hat auch für mich funktioniert, danke! Warum funktioniert LinearLayout nicht für einen FAB? – Michael

+1

Empfohlen wird: android: layout_alignParentEnd = "true" – Zon

1

Ich war in der Lage, den Code zu bekommen, indem Sie diese arbeiten:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:backgroundTint="#ff0000" 
    app:borderWidth="0dp" 
    app:elevation="8dp" 
    app:fabSize="normal" 
    android:layout_alignLeft="@+id/text" 
    android:layout_above="@+id/text"/> 

Es ist jedoch nicht mit below der TextView statt oben geklappt hat, ich glaube, es ist ein Fehler sein könnte.

+0

Ok, aber ich habe keine Textansicht, ich habe eine Listview, die den ganzen Bildschirm nehmen, also wenn ich es oben setze, werde ich nicht die gewünschte Position haben! –

5

Es scheint, dass FloatingActionButton in RelativeLayout nicht korrekt positioniert ist.

Ich änderte RelativeLayout zu FrameLayout, und Problem gelöst! Hoffe es hilft!

<ListView 
    android:id="@+id/mylist" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="1.0sp" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right|bottom" 
    android:src="@drawable/ic_add_white_48dp" 
    app:backgroundTint="@color/spg_rosa" 
    app:borderWidth="0dp" 
    app:elevation="8dp" 
    app:fabSize="normal" /> 

</FrameLayout> 
9

Layout sollte RelativeLayout sein. Versuchen Sie, den nächsten Code:

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

<LinearLayout 
    android:id="@+id/ly_list_form" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 


    <ListView 
     android:id="@+id/list_forms" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:dividerHeight="1dp" /> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/ly_bar_bottom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="right" 
    android:orientation="horizontal"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/button_addc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="28dp" 
     android:src="@drawable/ic_add" 
     app:borderWidth="0dp" 
     app:elevation="6dp" 
     app:pressedTranslationZ="12dp" /> 
</LinearLayout> 

+0

Das Schweben des schwebenden Knopfes in einem linearen Layout war der Trick für mich. Sonst würde es Ränder ignorieren. –

0

versuchen

android: layout_gravity = "bottom | right"

2

versuchen android.support.design.widget.CoordinatorLayout und android:layout_gravity="bottom|right" funktioniert bei mir mit.