有多个片段时在何处放置浮动操作按钮

时间:2019-01-08 23:53:11

标签: android android-layout

我正在尝试使用MainActivity创建一个布局,该布局包含一个BottomNavigationBar和一个用于换出片段的FrameLayout。我希望BottomNavigationBar保持固定,每个具有fab的片段在滚动时都会消失。问题是我不确定用于MainActivity和Fragments的布局以及将晶圆放置在何处,我可以将Fab放在MainActivity的布局中,并更改每个片段的Fab行为(不要知道如何)或在每个片段的布局中添加晶圆厂。当我执行后者时,我无法引用MainActivity的底部栏来停留在其上方。

我尝试了#1: MainActivity:

 <CoordinatorLayout>
 <FrameLayout/>
 <BottomNav/>
 <FAB/>
 </CoordinatorLayout>

和#2:

 <CoordinatorLayout>
 <FrameLayout/> -> fabs defined in each fragment layout
 <BottomNav/>
 </CoordinatorLayout>

我的Main activites xml文件如下所示:

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

还有我使用FAB的片段之一

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".View.MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/note_item"
        android:layout_gravity="top" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/button_add_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="64dp"
        android:layout_marginEnd="16dp"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/recycler_view"
        app:layout_anchorGravity="bottom|right|end"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我希望底部导航和协调器布局能够知道该晶圆厂,并且晶圆厂位于底部导航上方,而无需使用硬编码的dp值,但是我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果要使用CoordinatorLayout.Behavior逻辑,则您的晶圆厂应与CoorditorLayout处于同一级别。

我为您建议以下逻辑:活动包含BottomNavigation,FrameLayout(片段容器)。然后,您的所有片段都包含:

 <CoordinatorLayout>
 <RecyclerView/>
 <FloatingButton/>
 </CoordinatorLayout>

它将赋予您按照所需的方式构造片段UI的功能。

相关问题