全屏导航抽屉

10

我有一个导航抽屉,像这样

我想要的是将我的抽屉全屏打开而不是半屏。如何使这个抽屉成为全屏打开?

以下是drawerlayout的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:fitsSystemWindows="true">

<include
    android:id="@+id/toolbar_layout"
    layout="@layout/app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar_layout"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/activityMainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimary"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
6个回答

34
您可以尝试使用navigationView属性,它百分之百有效。
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:layout_marginEnd="-65dp"
    android:layout_marginRight="-65dp"
    app:itemTextColor="@color/lightgray"
    app:itemIconTint="@color/colorAccent"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

2

尝试这个,希望能够成功:

<include
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/drawer"
        android:layout_marginLeft="-64dp"/>

0
在主 DrawerLayout 类中有一个名为private static final int MIN_DRAWER_MARGIN = 64;的变量。
因此,要使完整的抽屉只需将android:layout_marginStart="-64dp"赋予导航视图或装载抽屉的容器即可。

0

这就是你可以实现它的方法。

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginEnd="-65dp"
        android:layout_marginRight="-65dp"
        android:fitsSystemWindows="true">
      />

0

你必须将你的DrawerLayout作为主要布局放在你的xml中,像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar_layout"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<RelativeLayout 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"
    android:fitsSystemWindows="true">

<include
    android:id="@+id/toolbar_layout"
    layout="@layout/app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/activityMainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimary"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/activity_main_drawer" />

</RelativeLayout>

</android.support.v4.widget.DrawerLayout>

这样做会将我的工具栏放到抽屉里面,而不是像照片中那样在顶部,这并不符合我的要求。我想要工具栏保持在drawerlayout的上方,并将侧边抽屉打开至全屏,而非半屏。 - Shrikant

-2

尝试将 android:layout_width="wrap_content" 更改为 android:layout_width="fill_parent"

这应该可以解决您的问题,请告诉我......


尝试了这个解决方案,但仍然没有覆盖整个屏幕。仍有一部分被遗漏了。 - Shrikant
fill_parent已被弃用。 - Sachin

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