2017-07-10 28 views
0

我的一些浮動動作按鈕不會留在我想要的地方,我不明白爲什麼。 enter image description here 首先是它們應該是怎樣的(實際上有時它是有效的),另外兩個圖像顯示它們有時會如何表現......並且我無法在這種行爲中找到「模式」。Android:浮動動作按鈕的定位不可預知

這是佈局(只有一個半):

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/lay_attivita_edit_ubicazioni" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingLeft="@dimen/list_padding_lateral" 
     android:paddingRight="@dimen/list_padding_lateral"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:id="@+id/container_attivita_edit_origini"> 
      <TextView 
       style="@style/FieldLabel" 
       android:layout_marginTop="20dp" 
       android:text="@string/attivita_edit_origini" /> 
      <ListView 
       style="@style/ListView" 
       android:id="@+id/list_attivita_edit_origini" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scrollbars="vertical" 
       android:fadeScrollbars="false" 
       android:background="@drawable/border_rectangle" /> 
     </LinearLayout> 
    </LinearLayout> 

    <android.support.design.widget.FloatingActionButton 
     style="@style/FloatingActionButton.Small" 
     android:id="@+id/fab_attivita_edit_ubicazioni_edit_origini" 
     app:layout_anchor="@id/list_attivita_edit_origini" 
     app:layout_anchorGravity="top|right|end" /> 
</android.support.design.widget.CoordinatorLayout> 

我也試圖SE anchoranchorGravity編程,但結果是一樣的。可能發生這種情況的原因在於,根據某些邏輯,兩個部分中的一個不可見,以防止在OnCreateView的末尾使它們不可見。

它不依賴於「錨列表」的可見性,因爲即使我總是讓兩個列表都可見,晶圓廠也會移動。 我也試着改變它們的編程錨點和anchorId在他們或他們的列表的每一個可見性的變化,什麼都沒有。

CoordinatorLayout.LayoutParams editOrigineFabParams = (CoordinatorLayout.LayoutParams)editOrigineFab.LayoutParameters; 
editOrigineFabParams.AnchorId = Resource.Id.list_attivita_edit_origini; 
editOrigineFabParams.AnchorGravity = (int)(GravityFlags.Top | GravityFlags.Right | GravityFlags.End); 
editOrigineFab.LayoutParameters = editOrigineFabParams; 

具有相同的條件/代碼有時他們工作,有時他們不。它看起來像Android中的一個錯誤。我嘗試使用Android 7.0和7.1,Xamarin.Android.Support.Compat.25.3.1。 任何有類似問題或有什麼可能是原因的人的想法?

+0

如果您更改FAB錨點視圖的可見性,該按鈕將會移動。如果你不希望它移動,確保它的錨點視圖保持相同的寬度。 –

+0

@AlexandreBOURETZ所以你認爲這是因爲當我隱藏我設置爲零的寬度和高度的容器時?我嘗試,非常感謝。 –

+0

不客氣,讓我知道它是否有效。 –

回答

1

使用FAB by james montemagno。

將james montemagno從nuget添加到您的項目中。 https://www.nuget.org/packages/Refractored.FloatingActionButton/

<Refractored.Fab.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_margin="16dp" 
     android:src="@drawable/ic_action_content_new" 
     app:app_colorNormal="@color/primary" 
     app:app_colorPressed="@color/primary_pressed" 
     app:app_colorRipple="@color/ripple" /> 

enter image description here

+0

謝謝,我會做一個嘗試。在我試圖更新所有軟件包之前,也許我會嘗試使用Android 7.1,以瞭解它是否是Android fab的錯誤。 –

+0

有用的答案,因爲你給了我一個替代方案,但我浪費了一個小時試圖使它工作,我總是有問題「沒有找到類」Refractored.Fab.FloatingActionButton「的路徑:DexPathList」。我放棄了,這個組件已經很老了,如果可能的話,我寧願保持這個標準。 –

+0

您正在收到該錯誤,因爲您沒有將該組件從nuget包含到您的項目中。 –

0

使用layout_gravity用於定位。

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:src="@drawable/ic_edit" 
    android:layout_marginRight="15dp" 
    android:layout_marginBottom="15dp"/> 
+0

它不起作用,我嘗試了layout_gravity和/或layout_anchorGravity的每個組合。該錯誤具有完全不確定的行爲。我在片段生命週期中如何使用它可能有問題。不管怎樣,謝謝你。 –

+0

完全刪除您的FAB代碼並粘貼此代碼。它正在處理我的系統。 –

+0

大概我沒有解釋清楚......我的佈局在一開始和大部分時間都是如此。但是,當我在標籤面板中重新放置我的片段時,其中一些移動到了錯誤的位置,但有時只是移動。 –