Navigation drawer behind Toolbar

一曲冷凌霜 提交于 2019-12-12 02:45:37

问题


I am currently trying to have my navigation drawer going on top of my toolBar but I cannot figure out what's wrong in my code. I checked a lot a lot on the web already but I still can't see what I did wrong.

Here is what I got so far:

 <?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >


        <!-- This LinearLayout represents the contents of the screen  -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- the Toolbar -->
            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:background="?attr/colorPrimaryDark">
            </android.support.v7.widget.Toolbar>


                <!-- Rest of the Content View -->
                <RelativeLayout
                    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:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    tools:showIn="@layout/activity_main"
                    tools:context=".MainActivity">


                </RelativeLayout>



            <FrameLayout
                android:id="@+id/flContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


        </LinearLayout>



        <LinearLayout
            android:layout_width="230dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:fitsSystemWindows="true">

            <!-- drawer content -->

            <android.support.design.widget.NavigationView
                android:id="@+id/nvView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="@android:color/white"
                app:menu="@menu/drawer_menu"
                />

        </LinearLayout>

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

I already changed my style.xml with this to replace Actionbar with Toolbar as I saw on other post...

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="android:windowActionBar">false</item>
        <!-- colorPrimary is used for the default action bar background -->
        <item name="windowActionModeOverlay">true</item>

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

回答1:


DrawerLayout works the same as FrameLayout i.e. those declared last have the highest Z value.

Not 100% but:

Flip your child views:

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >   

        <LinearLayout
            android:layout_width="230dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:fitsSystemWindows="true">

            <!-- drawer content -->

            <android.support.design.widget.NavigationView
                android:id="@+id/nvView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="@android:color/white"
                app:menu="@menu/drawer_menu"
                />

        </LinearLayout>

       <!-- This LinearLayout represents the contents of the screen  -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- the Toolbar -->
            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:background="?attr/colorPrimaryDark">
            </android.support.v7.widget.Toolbar>


                <!-- Rest of the Content View -->
                <RelativeLayout
                    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:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    tools:showIn="@layout/activity_main"
                    tools:context=".MainActivity">


                </RelativeLayout>



            <FrameLayout
                android:id="@+id/flContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


        </LinearLayout>



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



回答2:


check the following code:

<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>


    <!-- This LinearLayout represents the contents of the screen  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- the Toolbar -->
        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_height="56dp"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:background="?attr/colorPrimaryDark">
        </android.support.v7.widget.Toolbar>


            <!-- Rest of the Content View -->
            <RelativeLayout
                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:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="56dp"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                tools:showIn="@layout/activity_main"
                tools:context=".MainActivity">


            </RelativeLayout>



        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
       <!-- drawer content -->

        <android.support.design.widget.NavigationView
            android:id="@+id/nvView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            app:menu="@menu/drawer_menu"
            />

    </LinearLayout>

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



回答3:


FOUND IT! all I did was actually good, the problem was that when I created my project Android Studio created activity_main.xml AND content_main.xml...all my code was in content_main.xml which was include in activity_main.xml with:

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

So I erased the activity_main content and copied all my code from content_main.xml and then deleted this one (content_main) and TADAAA... I just actually don't know why it created those two .xml file and not just one at the first place... mystery remains



来源:https://stackoverflow.com/questions/34374382/navigation-drawer-behind-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!