带有BottomNavigationView的FloatingActionButton

13

我无法定位我的FAB。它应该在右下角,但在BottonNavigationView的上方。

1)我能否在coordinateLayout中不使用RelativeLayout来实现这一点?

2)给我看看如何做

3)我应该使用FrameLayout作为片段的容器吗?

<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

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

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_electricity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchorGravity="bottom|right|end"
    app:srcCompat="@drawable/ic_add_electro" />

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom">

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

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

result


https://github.com/aurelhubert/ahbottomnavigation/issues/38 - Aditya Vyas-Lakhan
问题重复。请查看此答案。 https://dev59.com/L1kS5IYBdhLWcg3w6qMz#61584002 - Abdul Qadir
2个回答

41

快速搜索FloatingActionButton.Behavior告诉我们以下内容。

@Override
public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp) {
    if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
        // If the developer hasn't set dodgeInsetEdges, lets set it to BOTTOM so that
        // we dodge any Snackbars
        lp.dodgeInsetEdges = Gravity.BOTTOM;
    }
}

由于BottomNavigationView也位于布局底部。将以下元素添加到您的BottomNavigationView中,CoordinatorLayout将自动处理插入和闪避。

app:layout_insetEdge="bottom"

1
我必须确保底部栏与 FAB 在同一个 xml 文件中(而不是在某个包含的子布局中)。 - Daniel Wilson
4
确切地说,要达到这个效果,BottomNavigationViewFloatingActionButton都必须是CoordinatorLayout的直接后代(子级)。 - LightYearsBehind

1
  1. If you stick it in a linear layout and move the bottom nav outside of the coordinator view, it will be below it on the screen.

  2. You could do it this way, but I prefer using an empty fragment container which I put in the hosting activity, and then place the fragment inside of it in code.

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
           app:popupTheme="@style/AppTheme.PopupOverlay">
    
        </android.support.v7.widget.Toolbar>
    
    </android.support.design.widget.AppBarLayout>
    
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    
    </FrameLayout>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_electricity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        app:srcCompat="@drawable/ic_add_electro"/>
    
    
    
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom">
    
    </android.support.design.widget.BottomNavigationView>
    </LinearLayout>
    

请检查 https://dev59.com/hqfja4cB1Zd3GeqPtEQu - Daniel Gomez Rico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接