7

ここにnewbのビット。私はAndroid開発の約2ヶ月ですが、他の環境では長年の開発経験があります。FloatingActionButton、layout_anchor、layout_gravity

大丈夫です。私はFloatingActionButtonを持っていましたが、それは私が期待していたところに現れていなかったか、それを望んでいました。 CoordinatorLayoutの中にあり、AppBarLayout/Toolbarと続き、ListViewに続いています。ここで

レイアウトです:

<?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> 

このレイアウトでは、画面は次のようになります。 FAB in wrong position

layout_gravity"bottom|end"を言います。私は"bottom|right"に変更しましたが、同じ結果が得られました。私は多くのチュートリアルを読んで、Stack Overflowを研究し、運がなかった。

私はカウンター私が読んだものに実行しているようだFAB要素app:layout_anchor="@id/Vehicle_ListView"、に記載されているアンカーを削除することによってそれを解決するために管理:FABを使用し、それを適切に配置するために、あなたがlayout_anchorlayout_gravityを使用する必要があります。アンカータグがなければ、それは次のようになります。

FAB in correct position

だからここに私の質問です:なぜ私のアンカーは、私のFloatingActionButtonのポジショニングを台無しにされましたか?私は間違って何をしていますか?

答えて

11

-bosterあなただけlayout_anchorGravityを追加する必要があります。

<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" /> 
関連する問題