7

Ein bisschen von einem Newb hier. Ich bin etwa zwei Monate in Android-Entwicklung, aber ich habe jahrelange Erfahrung in anderen Umgebungen.FloatingActionButton, layout_anchor und layout_gravity

Okay. Ich habe eine FloatingActionButton, die nicht angezeigt wurde, wo ich es erwartet habe oder wollte. Es ist in einem CoordinatorLayout, zusammen mit einem AppBarLayout/Toolbar, und einem ListView folgend.

Hier ist das 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:id="@+id/fragment_coordinator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".ViewVehicleList"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      android:title="Vehicle List" 
      app:layout_scrollFlags="scroll|enterAlways|snap" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    </android.support.design.widget.AppBarLayout> 

    <ListView 
     android:id="@+id/Vehicle_ListView" 
     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" 
     android:background="#FFFFFF" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    </ListView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_AddVehicle" 
     style="@style/FloatingAddButton" 
     android:src="@drawable/ic_green_add" 
     android:layout_gravity="bottom|end" 
     app:layout_anchor="@id/Vehicle_ListView" 
     android:onClick="addVehicle"/> 

</android.support.design.widget.CoordinatorLayout> 

Mit diesem Layout, sieht der Bildschirm wie folgt: FAB in wrong position

Mein layout_gravity sagt "bottom|end". Ich änderte es zu "bottom|right", aber erhielt das gleiche Ergebnis. Ich habe viele Tutorials gelesen und Stack Overflow recherchiert und hatte kein Glück.

ich es geschafft, es zu lösen, indem sie den Anker im FAB-Element aufgelistet Entfernen app:layout_anchor="@id/Vehicle_ListView", die Zähler zu laufen scheint, was ich gelesen habe: eine FAB zu nutzen und positionieren es richtig Sie layout_anchor und layout_gravity verwenden müssen. Ohne den Anker-Tag, sieht es wie folgt aus:

FAB in correct position

Also hier meine Frage: Warum ist mein Anker die Positionierung meiner FloatingActionButton vermasseln? Was mache ich falsch?

-boster

Antwort

11

Sie müssen nur layout_anchorGravity hinzuzufügen.

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab_AddVehicle" 
    style="@style/FloatingAddButton" 
    android:src="@drawable/ic_green_add" 
    android:onClick="addVehicle" 
    app:layout_anchor="@id/Vehicle_ListView" 
    app:layout_anchorGravity="bottom|end" /> 
Verwandte Themen