带有BottomNavigationView的FloatingActionButton [英] FloatingActionButton with BottomNavigationView

查看:69
本文介绍了带有BottomNavigationView的FloatingActionButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I cant position my FAB. It should be bottom right, but on top of the BottonNavigationView.

1)我可以在coordinateLayout中没有RelativeLayout的情况下实现此功能吗?

1) Can i achieve this without RelativeLayout inside coordinateLayout?

2)向我展示如何

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

3) Should i use FrameLayout as a container for fragments?

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

推荐答案

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

A quick search to FloatingActionButton.Behavior tells us the following thing.

@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 将自动为您处理插入和闪避。

Since BottomNavigationView also resides at the bottom of the layout. Add following element to your BottomNavigationView and CoordinatorLayout will handle the inset and dodging for you automatically.

app:layout_insetEdge="bottom"

这篇关于带有BottomNavigationView的FloatingActionButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆