带有片段和底部导航栏的 Android FloatingActionButton

8
我正在创建一个具有以下结构的Android应用程序:
  • 主活动有一个底部导航栏,在3个不同的片段之间切换
  • 其中2个片段将显示项目列表,并带有一个浮动操作按钮(FAB)以添加新项目
  • 第三个片段将显示不同的内容,不需要FAB。
基于这个,似乎FAB应该“属于”列表片段,而不是主活动。
我目前遇到的问题是,FAB会隐藏在底部导航栏后面,就像这个问题中所示。那里有一个好的解决方案,但它依赖于FAB与底部导航栏在同一个布局中。
是否有一种方法可以使FAB不隐藏在底部导航栏后面,而不将FAB移动到主活动中(这会破坏片段的模块化)?
我的活动布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_insetEdge="bottom"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

在下面的示例中,"fragment container" 将被所选片段替换:

    TodoListFragment fragment = new TodoListFragment();

    //if we were started with an intent, pass on any special arguments
    fragment.setArguments(getIntent().getExtras());

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commit();

最后,该片段的布局如下:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView 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/list"
        android:name="..."
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="LinearLayoutManager"
        tools:context=".TodoListFragment"
        tools:listitem="@layout/fragment_todolist_item" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_add_white_24dp"
        android:layout_margin="16dp" />

</android.support.design.widget.CoordinatorLayout>
1个回答

8

问题出在你的约束条件上。你将FrameLayout的高度设置为与父级匹配。尝试这个方法 -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/navigation"/>

1
这个问题比较棘手,因为当你在使用Android Studio的项目创建向导并选择以BottomNavigation应用程序开始时,Google预配置的活动布局将片段视图的高度设置为match_parent而不是0dp - Cody

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