45

如何使抽屜佈局位於操作欄/工具欄下方?我正在使用v7:21應用程序compat庫和新的ToolBar視圖。如何讓DrawerLayout顯示在工具欄下方?

例子,我看到的樣子

<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"> 

    <!-- drawer content --> 

</LinearLayout> 

<!-- normal content view --> 
<LinearLayout 
    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" /> 

    <!-- The rest of content view --> 

</LinearLayout> 

但隨後的工具欄將被抽屜,這使得動畫漢堡包圖標(如v7.ActionBarDrawerToggle)被隱藏無用的,因爲它不會是在抽屜下方可見,但我確實想使用新的工具欄視圖來更好地支持Material主題。

那麼如何做到這一點呢?是否可以將DrawerLayout作爲非頂級視圖?

+2

http://www.journaldev.com/12648/navigati onview-android – AnupamChugh 2016-09-19 08:24:52

+0

這可能有助於http://www.gadgetsaint.com/android/android-navigation-drawer-toolbar/#.WOBABBJ97fY – ASP 2017-04-02 00:11:30

+0

@AnupamChugh - 您在上面提供的鏈接(** journaldev.com/12648/navigationview- android **)的確是我想要的。謝謝!! – 2018-03-07 10:03:37

回答

93
<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"> 

    <!-- 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"> 

      <!-- drawer content --> 

     </LinearLayout> 

     <!-- normal content view --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 



      <!-- The rest of content view --> 

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

</LinearLayout> 
+4

歡迎來到SO!請給你的答案添加一些解釋。 – 2014-10-24 19:57:40

+2

這樣做對我來說,一旦實施,似乎很明顯。工具欄需要放在抽屜佈局上,而不是內部,然而Android開發人員在抽屜的根部有一個抽屜的例子,因爲默認情況下,操作欄會超出頂部。 – NathofGod 2014-10-26 11:21:41

+0

這也適用於我。有意義 - 因爲工具欄現在被認爲是層次結構的一部分(與舊的操作欄相對),佈局中的實際位置變得更加重要。 – 2014-10-27 07:09:05

21

您可以使用自定義的toolbar

,但周圍的工作將設置一個top_margin到抽屜我不認爲。 (對谷歌Play商店一樣的東西!)

<!-- drawer view --> 
<LinearLayout 
    android:layout_marginTop="?attr/actionBarSize" 
... 

,如果你找到了一個更好的解決辦法告訴我太;)

+3

似乎工作的一種解決方案是重新排列布局,使用LinearLayout作爲佈局,將ToolBar和DrawerLayout作爲子項。 – user1309971 2014-10-21 08:48:23

+1

這是更好的解決方案。謝謝隊友 – Darsh 2016-05-06 11:27:36

+1

解決我的問題...謝謝.. – Ironman 2016-07-05 06:45:10

5

改變你的抽屜佈局風格類似如下

RelativeLayout 
----Toolbar 
----DrawerLayout 
    ---ContentView 
    ---DrawerList 
1

我有問題得到這個工作,並最終正確顯示。下面是我使用的佈局:

<LinearLayout...[add your namespaces, etc] 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:fitsSystemWindows="true"> 

    <android.support.v7.widget.Toolbar [your custom toolbar] /> 
    <android.support.v4.widget.DrawerLayout [your drawer layout] /> 
     <LinearLayout> 
      [content here] 
     </LinearLayout> 
    <android.support.design.widget.NavigationView /> 
</LinearLayout> 

小心走動的設計部件,如果是錯誤的根標籤下,你會得到一個

「沒有layout_gravity_left

異常。

4

我的解決辦法:生成模板與Android 2.1.2工作室,抽屜模板:

只需要三個轉變:設定保證金頂鑑於NavigationView和style.xml刪除overloap狀態欄

<item name="android:windowDrawsSystemBarBackgrounds">false</item> 

android:fitsSystemWindows="false" 

佈局main.xml中設置的空白得到最高的大小動作條價值 android:layout_marginTop="?attr/actionBarSize"

<?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_gravity="start" 
     android:layout_marginTop="?attr/actionBarSize" 
     android:fitsSystemWindows="false" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

** false **樣式設置僅適用於API Level 21及更高版本。我總是在API Level 17的基礎上編寫我的應用程序。所以這個解決方案對我來說不起作用... – 2018-03-07 09:52:10

+0

嘗試放入values-21/styles.xml – Webserveis 2018-03-07 09:59:47

+0

它適用於我的API Level 17應用程序, * values-21/styles.xml **但系統窗口的頂部不再遵循應用程序主題,但有一個黑色的背景,這絕對不是我想要的應用程序的外觀。我試過這個解決方案** https://www.journaldev.com/12648/navigationview-android**,它對我來說非常合適。 – 2018-03-07 10:11:41