1

我想在我的應用程序中導航抽屜,但我不想讓導航抽屜蓋住我的操作欄。操作欄應始終可見。我搜索互聯網,並從stackoverflow和一些博客得到了一些解決方案,但都是一樣的。我提到這個解決方案Using Navigation Drawer without TitleBar or ActionBar,但仍然是相同的。正如他們在答案中提到的,我先給android.support.v7.widget.Toolbar然後android.support.design.widget.NavigationView。 Android工作室設計預覽顯示正確,但實時手機操作欄由導航抽屜覆蓋。導航抽屜蓋操作欄

在Android Studio中

Android Studio

手機

enter image description here

這裏是activity_album_dashboard.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"> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context=".activity.AlbumDashboardActivity"> 

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

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

    <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:headerLayout="@layout/nav_header_album_dashboard" 
     app:menu="@menu/activity_album_dashboard_drawer" /> 

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

和nav_header_album_dashboard.xml(我不想頭,所以就一直空的LinearLayout)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

</LinearLayout> 
+0

設置您的導航視圖,添加layout_below:工具欄 –

+0

檢查我的答案我更新了你的XML。只需將其複製並粘貼到佈局文件中 –

+0

從抽屜佈局中刪除此行android:fitsSystemWindows =「true」 –

回答

2

你需要把你抽屜佈局工具欄後......只是改變你的佈局是這樣的...如果你不需要頭那麼爲什麼使用頭部任何佈局剛剛從導航視圖中刪除app:headerLayout="@layout/nav_header_album_dashboard"

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".activity.AlbumDashboardActivity"> 

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

<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" 
    tools:openDrawer="start"> 

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

    <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:headerLayout="@layout/nav_header_album_dashboard" 
     app:menu="@menu/activity_album_dashboard_drawer" /> 

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

此解決方案適用於我。但**狀態欄**被導航抽屜覆蓋。我在'android.support.v4.widget.DrawerLayout'中刪除了'android:fitsSystemWindows =「true」',它的工作正常。 –

+0

@UnnikuttanKaveth我更新了我的代碼.......... –

0

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".activity.AlbumDashboardActivity"> 

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

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

<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" 
    app:headerLayout="@layout/nav_header_album_dashboard" 
    app:menu="@menu/activity_album_dashboard_drawer" /> 

在NavigationView中設置的margintop等於工具欄的大小。它爲我工作