如何确保CoordinatorLayout子级不会重叠

时间:2018-12-29 18:43:50

标签: android material-design android-coordinatorlayout android-bottomappbar

我正在协调器布局内使用底部的应用程序栏。其余内容来自片段。片段内容被底部的应用栏覆盖

因为我正在使用bottomAppBar,所以它必须位于CoordintorLayout中,所有子项也必须位于其内部。 BottomAppBar上方的所有布局均应填充空间提示,但如果这样做,底部的视图将被覆盖。如何确保视图与BottomAppBar不重叠

<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawerLayout_main"
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=".main.MainActivity">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <fragment
        android:id="@+id/fragment_main_navHost"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_gravity="top"
        app:navGraph="@navigation/nav_graph"/>


    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabAlignmentMode="end"
        app:fabCradleMargin="4dp"
        app:hideOnScroll="true"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_layout"
    app:menu="@menu/main_navigation"/>

我需要确保要替换片段的内容不被底部应用程序栏覆盖,并且应使用底部应用程序栏未使用的所有空白。在另一个世界中,在使用其余空白时内容应位于其上方,并且仍位于协调器布局内。

3 个答案:

答案 0 :(得分:1)

只需将margin添加到片段容器中即可:

  <fragment
    android:id="@+id/fragment_main_navHost"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="{bottom_navigation_height}"
    android:layout_marginTop="{tool_bar_height}"
    app:defaultNavHost="true"
    android:layout_gravity="top"
    app:navGraph="@navigation/nav_graph"/>

答案 1 :(得分:0)

根据CoordinatorLayout Documentation,可以用layout_dodgeInsetEdges=”bottom”指定CoordinatorLayout中的View希望与另一个View重叠的边缘。因此,如果您的片段被BottomAppBar覆盖,则可以将此属性添加到片段中。您还必须指定您希望避开BottomAppBar。这是通过向其中添加layout_insetEdge=”bottom”来完成的。

答案 2 :(得分:0)

尝试将<ListView x:Name="MyList"> . . . <ListView.ItemContainerTransitions> <TransitionCollection> <!-- Empty collection to not have any transitions interrupt the composition Item animations --> </TransitionCollection> </ListView.ItemContainerTransitions> . . . </ListView> 添加到片段中。