如何将导航抽屉放在工具栏下方?

10

这里我的导航抽屉在工具栏之上。我也添加了一些XML代码。请帮帮我。

这里我的导航抽屉在工具栏之上

这是我的activity.xml

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

    <include
        layout="@layout/app_bar_categories"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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:fitsSystemWindows="true"
        app:menu="@menu/activity_main2_drawer" />

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

和我的app_bar xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true"
    tools:context="com.ezybzy.ezybzy.categoris">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_categoris" />

</android.support.design.widget.CoordinatorLayout>

和我的内容main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ezybzy.ezybzy.categoris"
    tools:showIn="@layout/app_bar_categories"
    android:background="#ffffff">
</RelativeLayout>

我使用Android Studio的导航抽屉活动创建了导航抽屉。


您可以参考这篇文章 - http://stackoverflow.com/questions/31059761/navigation-drawer-below-tool-bar-in-material-design - Inducesmile
请查看此网站,它将帮助您编写代码:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ - Vishal Thakkar
9个回答

11

当然,android:layout_marginTop="?attr/actionBarSize"可以胜任此工作。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

但问题在于drawerlayout在工具栏之上,这就是为什么会出现淡化的情况。 您可以通过删除来解决淡化问题。

mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));

但在某些设备上可能看起来有点奇怪。

解决方案

当使用Android Studio时,我们可以创建NavigationDrawerActivity。有三个文件分别命名为:

activity_main.xml

app_bar_main.xml

nav_header_main.xml

content_main.xml

所以我们可以跳过app_bar_main.xml并删除淡入效果。

步骤1

将活动主视图的根视图设置为垂直LinearLayout

<LinearLayout 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:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="com.example.MainActivity">
</LinearLayout>

activity_main.xml 中添加 DrawerLayout 并将 content_main.xml 包含在 DrawerLayout 中。在 DrawerLayout 上方添加 AppBarLayout

<LinearLayout 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:orientation="vertical"
tools:context="com.qproinnovations.schoolmanagement.activity.HomeActivity">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    <!-- drawer view -->
        <include layout="@layout/content_main" />
<!-- drawer content -->
        <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:fitsSystemWindows="true"
            app:menu="@menu/activity_home_drawer" />
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

步骤2

将NavigationDrawerActiviity的setContentView()方法添加或替换为:

setContentView(R.layout.activity_main);

最终我们有了:

输入图像描述


1
救命稻草。谢谢。 - Kenny Dabiri

7

添加

android:layout_marginTop="?attr/actionBarSize"

将其添加到您用作抽屉的布局中。

当我将此内容添加到导航视图中时,它可以工作。但是当导航抽屉出现时,操作栏会变得逐渐消失.. 我不希望这样。 - Kiran Benny Joseph
请参考此链接以移除ActionBar的淡入淡出效果:https://dev59.com/SGct5IYBdhLWcg3wPLCB - pRaNaY
2
这个方法加上将android:fitsSystemWindows设置为“false”,对于我来说可以让抽屉在工具栏下面。 - ProgDevCode
@ProgDevCode 很高兴能为您提供帮助。 - pRaNaY

2
如果您正在使用自定义工具栏,则可以按照以下方式使用抽屉布局...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <!-- The toolbar -->
    <android.support.v7.widget.Toolbar  
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <!-- drawer view -->
        <LinearLayout
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start">
            ....
        </LinearLayout>

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

</LinearLayout>  

如果您没有使用自定义工具栏,则需要将顶部边距设置为抽屉布局。
android:layout_marginTop ="?android:attr/actionBarSize"

2
在您的导航抽屉布局文件中,您应该将android:layout_marginTop ="?android:attr/actionBarSize"添加到容器中。

1
但是当导航抽屉出现时,操作栏会淡化...我不希望这个操作栏淡化。 - Kiran Benny Joseph

2
一个简单而好的解决方案是将fitsSystemWindows=false设置为:
android.support.v4.widget.DrawerLayout

具有ID为的

android:id="@+id/drawer_layout"

对于navigationView,将layout_marginTop设置为?attr/actionBarSize,这将获取操作栏的大小并将其设置为边距。

这是完整的activity_main.xml代码,其中包含上述两个更改。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="false"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"

        app:menu="@menu/activity_main_drawer" />
    <!--app:headerLayout="@layout/nav_header_main"-->
</android.support.v4.widget.DrawerLayout>

因为打开导航抽屉时整个布局会淡化。 - Kiran Benny Joseph
正确的方法是只淡化布局重叠的部分。 - Kiran Benny Joseph
这不是由于我在上面的代码中所做的更改,而是DrawerLayout的默认行为。 - Inzimam Tariq IT
但是这可以通过这个问题的答案来实现。 - Kiran Benny Joseph
如果您不想使用淡入淡出效果,请使用蒙版颜色。 mDrawerLayout.setScrimColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent)); - Inzimam Tariq IT
当打开导航抽屉时,所有的淡入效果都将消失,这个操作没有任何影响。 - Kiran Benny Joseph

1
创建一个像这样的布局:
 <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:elevation="4dp"
        android:layout_height="fill_parent"
        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"

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


            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/bg_new">


    <put your layout here................>

            </FrameLayout>



        </LinearLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:background="@drawable/bg_all"

            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:theme="@style/list_item_appearance"
            app:menu="@menu/drawer_menu" >


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

1

如果想避免抽屉覆盖工具栏,请尝试以下代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="start"
        android:background="@android:color/transparent"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/toolBarStyle"
        app:titleTextAppearance="@style/Toolbar.TitleText" />

    <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"
        android:layout_below="@+id/toolbar"
        android:fitsSystemWindows="true">

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            android:background="@android:color/transparent" />

        <fragment
            android:id="@+id/navigation_drawer"
            class="com.buzzintown.consumer.drawer.NavigationDrawerFragment"
            android:layout_width="310dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/drawer_layout" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:openDrawer="start">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"

android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <!--content_main is my layout you can design your own-->
   <!--one more thing is dont put toolbar in your content_main layout-->
    <include layout="@layout/content_main" />

    <FrameLayout
        android:id="@+id/content"
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/activity_main_drawer" />

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


</LinearLayout>

0

您可以更改导航抽屉的XML android:layout_marginTop ="0dp",以在工具栏中设置顶部边距


但是当导航抽屉出现时,操作栏会淡出..我不想要这种淡出效果.. - Kiran Benny Joseph

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