簡體   English   中英

浮動操作按鈕位置錯誤

[英]Floating Action Button in wrong position

我在我的兩個Fragment布局中有浮動動作按鈕,有時它們移動到錯誤的位置,盡管我使用CoordinatorLayout。

這應該是它的樣子

當我打開碎片時,這就是它看起來的樣子

這是我片段的代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout android:id="@+id/notes_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.NotesFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/notes_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/new_note_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_add_black_48dp"
    app:layout_anchor="@id/notes_layout"
    app:layout_anchorGravity="bottom|center"
    android:onClick="openNewNote"/>

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

有誰知道為什么FAB有時會走向錯誤的位置?

我遇到了同樣的問題。 問題在於app:layout_anchorcom.android.support.design:24.2.0+的組合

我能夠通過從fab中刪除app:layout_anchor屬性或者恢復到舊版本的設計庫來解決此問題。 24.1.1對我來說很好。 我還必須將appcompat v7庫還原為24.1.1

我希望這有幫助。

嘗試讓FAB成為CoordinatorLayout第一個孩子

<android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            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">

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

       <android.support.v7.widget.RecyclerView
             .../>

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

CoordinatorLayout.LayoutParams類具有anchorGravity字段。 例如:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.anchorGravity = Gravity.BOTTOM | GravityCompat.START;
fab.setLayoutParams(lp);

在我的情況下,這是24.2.0及更高版本的支持庫中的錯誤。 因此,解決它的唯一方法是使用支持lib v24.1.1構建應用程序。

嘗試這個:

    <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            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">

       <android.support.v7.widget.RecyclerView
             android:id="@+id/rvToDoList"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

      </android.support.v7.widget.RecyclerView>

       <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@mipmap/ic_launcher"
            app:layout_anchor="@id/rvToDoList"
            app:layout_anchorGravity="bottom|right|end"/>

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

這些建議都沒有結束為我工作(降級不是一個選項;移動FAB,因此它是第一個讓它在Android 14上看不見的孩子;刪除layout_anchor使它一直跳到隨機位置)

我改為使用ConstraintLayout在我想要的兩個視圖之間手動定位FAB。 由於我的整個屏幕沒有滾動區域,因此工作正常。

要以這種方式定位FAB,請將其頂部和底部約束設置為它應該重疊的上部視圖之一的底部,並確保它是ConstraintLayout中的最后一個子視圖。

將FAB放在RelativeLayout中,它對我有用,我使用的是支持庫的25.3.1版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM