【问题标题】:Navigation Drawer covers my toolbar导航抽屉覆盖了我的工具栏
【发布时间】:2015-08-14 09:50:00
【问题描述】:

我正在使用这个材料设计导航抽屉的例子,问题是,当我打开我的抽屉时,它也覆盖了我的操作栏..我的抽屉图标不可见..这就是我得到的..以下是我的代码..

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


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

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />


    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

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

【问题讨论】:

标签: android android-actionbar navigation-drawer material-design


【解决方案1】:

我建议使用垂直方向的线性布局作为父级,然后第一个子级是您的工具栏,第二个是您的抽屉布局。

linearlayout vertical
  toolbar
  drawerlayout

希望这会有所帮助。

【讨论】:

  • 用xml发帖,这样也能帮助别人
【解决方案2】:

您在android.support.v4.widget.DrawerLayout 中的任何内容都将被导航抽屉覆盖。看到您在android.support.v4.widget.DrawerLayout 中有您的工具栏,所以它位于导航抽屉下方。你所要做的就是让它远离android.support.v4.widget.DrawerLayout。这些都是视图,因此请设计布局,使工具栏位于android.support.v4.widget.DrawerLayout 及其子视图之上。

但是我应该声明,在 Android 5.0 Lollipop 之后,根据材料指南,导航抽屉应该超过工具栏。但是你没有必要这样做。

这是一个示例布局 (但是,理解理论部分是必要的,因为之后您可以创建任何类型的布局)

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

【讨论】:

  • 你能用 xml 作为答案发布,这样它会对其他人有所帮助
  • @Lakhan 我添加了一个示例布局。看到工具栏在 DrawerLayout viewGrouo 之外。所以抽屉不会越过工具栏。
【解决方案3】:

这是最新导航视图的示例。

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

    <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-->
    <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 with menu items defined in activity_main_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>

【讨论】:

    【解决方案4】:

    您可以使用Listview 代替fragment,它将在Actionbar 下方制作导航抽屉。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                style="@style/MyToolBarStyle.Base"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize" />
        </LinearLayout>
    
        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
            <!-- activity view -->
            <!-- Framelayout to display Fragments -->
            <FrameLayout
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <!-- navigation drawer -->
            <ListView
                android:id="@+id/list_slidermenu"
                android:layout_width="@dimen/slider_dimen"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="@android:color/white"
                android:divider="#000"
                android:dividerHeight="1dp" />
    
        </android.support.v4.widget.DrawerLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案5】:
      Modifying your code for the result you expect:
      
      <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">
      
        <include
                  android:id="@+id/toolbar"
                  layout="@layout/toolbar" />
      
      <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">
      
         <FrameLayout
              android:id="@+id/container_body"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="1" />
      
          <fragment
          android:id="@+id/fragment_navigation_drawer"
          android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
          android:layout_width="@dimen/nav_drawer_width"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:layout="@layout/fragment_navigation_drawer"
          tools:layout="@layout/fragment_navigation_drawer" />
      
         </android.support.v4.widget.DrawerLayout>
      
      </RelativeLayout>
      
      
      Your Welcome...
      

      【讨论】:

        猜你喜欢
        • 2017-08-03
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        • 2014-12-23
        • 2014-10-31
        • 2015-02-26
        • 2015-01-15
        • 2016-03-26
        相关资源
        最近更新 更多