我想打开一个抽屉,使其覆盖操作栏。 我尝试使用父线性布局,并在其中定义了工具栏和抽屉布局并且工作正常,但问题是我在那里看不到菜单项.. 也就是说,我只能通过在屏幕上从左向右滑动来打开抽屉。 如果我在线性布局之外定义工具栏,则菜单显示的只是操作栏未被抽屉覆盖。如何同时实现两者?这是我的XML文件Activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="variofitness.com.schedulekeeper.HomeActivity">
<include
android:id="@+id/toolbar_actionbar"
layout="@layout/toolbar_default"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true" />
<fragment
android:id="@+id/fragment_drawer"
android:name="variofitness.com.schedulekeeper.Fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
尝试这种方式会有所帮助
<android.support.v4.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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Please modify your Xml:Put your action bar inside the drawer or use drawer as parent layout:
另一个示例代码:<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_all_courses_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.activlearn.ui.AllCoursesActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<include layout="@layout/home_tool_bar" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_all_courses" />
</android.support.design.widget.CoordinatorLayout>
<fragment
android:name="com.activlearn.fragments.DrawerFragment"
class="com.activlearn.fragments.DrawerFragment"
android:layout_width="@dimen/drawer_width"
android:layout_height="wrap_content"
android:layout_gravity="start"
tools:layout="@layout/fragment_drawer" />
</android.support.v4.widget.DrawerLayout>
如果您获得正确的布局结构,这应该可以正常工作。根据此处的本指南:
若要添加导航抽屉,请将带有 DrawerLayout 对象的用户界面声明为布局的根视图。在 DrawerLayout 中,添加一个包含屏幕主要内容的视图(隐藏抽屉时的主要布局)和另一个包含导航抽屉内容的视图。
因此,您的抽屉布局应该是根目录,并且有两个直接子级。但是,您需要包含三个元素,因此您需要将工具栏和内容容器组合在一起。在伪代码中,结构如下所示:
<DrawerLayout>
<LinearLayout> (or other type of layout)
<Toolbar/>
<content container/>
</LinearLayout>
<Navigation Drawer/>
</DrawerLayout>