使用androidx.drawerlayout.widget.DrawerLayout时如何将fragment容器放在toolbar下方
问题根本原因
DrawerLayout继承自FrameLayout,所有直接子View默认会从布局左上角开始绘制,你当前将Toolbar和FragmentContainerView都作为DrawerLayout的直接子View,自然会出现重叠。另外你添加的app:layout_behavior属性仅在父布局为CoordinatorLayout时生效,当前场景下该属性无效。
可行解决方案
方案1:无滚动联动需求的最简实现(推荐入门场景使用)
直接将主内容区域用垂直方向的LinearLayout包裹,先放Toolbar再放Fragment容器,结构如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout 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/drawer" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DashboardActivity"> <!-- 主内容区域:单个垂直排列的容器 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:minHeight="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ToolbarTheme" app:titleTextColor="@android:color/white" android:background="?attr/colorPrimary"/> <androidx.fragment.app.FragmentContainerView android:id="@+id/dashboardFragmentContainerView" android:background="@color/black" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <!-- 侧边导航栏,必须设置layout_gravity --> <com.google.android.material.navigation.NavigationView android:id="@+id/navigation_view" android:fitsSystemWindows="true" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:clipToPadding="false" app:menu="@menu/main_menu"/> </androidx.drawerlayout.widget.DrawerLayout>
方案2:需要Toolbar和Fragment滚动联动效果的实现
如果后续需要实现滚动时Toolbar折叠/显示的效果,使用CoordinatorLayout+AppBarLayout结构,此时behavior属性才会生效:
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout 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/drawer" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DashboardActivity"> <!-- 主内容区域:CoordinatorLayout作为根容器 --> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:minHeight="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ToolbarTheme" app:titleTextColor="@android:color/white" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways"/> </com.google.android.material.appbar.AppBarLayout> <androidx.fragment.app.FragmentContainerView android:id="@+id/dashboardFragmentContainerView" android:background="@color/black" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> <!-- 侧边导航栏 --> <com.google.android.material.navigation.NavigationView android:id="@+id/navigation_view" android:fitsSystemWindows="true" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:clipToPadding="false" app:menu="@menu/main_menu"/> </androidx.drawerlayout.widget.DrawerLayout>
使用该方案需要确保你的项目已经引入了Material组件库和AndroidX CoordinatorLayout依赖。
内容的提问来源于stack exchange,提问作者Pritish
相关产品推荐
相关产品推荐

