2015-08-20 4 views
9

Ich habe einen FloatingActionButton. Ich möchte es in der Mitte von 2 LinearLayouts und der Mitte des Bildschirms so sein. enter image description hereSo richten Sie den Floating Action Button an der Mitte aus

Derzeit Mein Design ist wie dieser

enter image description here

ich es genau in der Mitte des Bildschirms sein wollen. Wie geht das?

Das ist meine ganze XML-Code

<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"> 
<LinearLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:id="@+id/viewA" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.6" 
     android:orientation="horizontal"/> 
    <LinearLayout 
     android:id="@+id/viewB" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2" 
     android:orientation="horizontal"/> 
</LinearLayout> 
<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:src="@drawable/ic_plus" 
    app:layout_anchor="@id/viewA" 
    android:layout_centerHorizontal="true" 
    app:layout_anchorGravity="bottom" 
    android:layout_centerInParent="true" /> 

+1

Können Sie Ihre gesamte XML-Layout posten? –

+0

Ich habe die Frage bearbeitet. Mein ganzer XML-Code ist da – dev

+0

Funktioniert 'app: layout_anchorGravity =" center "? –

Antwort

10

Ihre CoordinatorLayout ein FrameLayout ist, so versuchen auf diese Weise, Ich hoffe, es wird hilft Sie .

app:layout_anchorGravity="bottom|center" 
+3

Dies sollte funktionieren, da CoordinatorLayout ein FrameLayout ist, kein RelativeLayout wo 'layout_centerHorizontal =" true "' funktionieren würde. – Zharf

+0

@Zarf Ja. Danke für die Freigabe –

0

Änderung dieser

app:layout_anchor="@id/viewA" 
android:layout_centerHorizontal="true" 
+0

Nein, es funktioniert nicht – dev

1

Einstellung layout_centerHorizontal und layout_centerInParent wird in einem CoordinatorLayout nicht funktionieren. Sie sind nur in einem RelativeLayout erlaubt. Sie sollten es tun, wie folgt:

app:layout_anchorGravity="center|bottom" 
1

Above Lösungen nicht für mich irgendwie arbeiten. Ich änderte die android:layout_gravitiy zu

android:layout_gravity="bottom|center"

Hoffe, es hilft :-)