Android 固定导航抽屉中的导航标题,而滚动通过项目

Android 固定导航抽屉中的导航标题,而滚动通过项目,android,navigation-drawer,android-navigationview,Android,Navigation Drawer,Android Navigationview,当前状态:带有NavigationHeader和NavigationMenu项的NavigationDrawer。项目数量很大,因此需要滚动才能访问底部的项目 要求:向下滚动至底部时,NavigationHeader应保持固定 这是我的活动\u主布局文件 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sc

当前状态:带有NavigationHeader和NavigationMenu项的NavigationDrawer。项目数量很大,因此需要滚动才能访问底部的项目

要求:向下滚动至底部时,NavigationHeader应保持固定

这是我的活动\u主布局文件

<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_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:fitsSystemWindows="true"
    android:background="#00000000"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"
    />

</android.support.v4.widget.DrawerLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="#3d3b3b"

android:theme="@style/ThemeOverlay.AppCompat.Dark">

<android.support.v7.widget.AppCompatImageView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:src="@drawable/categories"
    android:padding="1dp"
    />

</RelativeLayout>

这是我的导航\u标题\u主布局文件

<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_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:fitsSystemWindows="true"
    android:background="#00000000"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"
    />

</android.support.v4.widget.DrawerLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="#3d3b3b"

android:theme="@style/ThemeOverlay.AppCompat.Dark">

<android.support.v7.widget.AppCompatImageView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:src="@drawable/categories"
    android:padding="1dp"
    />

</RelativeLayout>

另外,我是android开发新手。如果需要,请请求更多资源

  • 从RelativeLayout中删除方向。它不支持它
  • 为标题视图的父相对视图设置android:gravity=“bottom”

  • 我的错误,它仍然会滚动。您需要为导航菜单设置自定义回收器视图。所以它在自己的容器中滚动

    找到了一个解决方法。绝对不是最有效的。请建议是否可以从这里做些什么

    <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_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:fitsSystemWindows="true"
      android:background="#00000000"
      app:headerLayout="@layout/nav_header_main"
      app:menu="@menu/activity_main_drawer">
    
     <include layout="@layout/nav_header_main"
    
     <android.support.design.widget.NavigationView>
    
    
    android:id=“@+id/nav_视图”
    android:layout\u width=“包装内容”
    android:layout\u height=“match\u parent”
    android:layout\u gravity=“开始”
    android:fitsSystemWindows=“true”
    android:background=“#00000000”
    app:headerLayout=“@layout/nav_header_main”
    app:menu=“@menu/activity\u main\u drawer”>
    
    只需将导航视图包装成线性布局


    我知道这个问题太老了,但我分享了一个适合我的解决方案:

    1。将导航标题主视图放在导航视图内

    <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_main_drawer" >
    
        <include layout="@layout/nav_header_main"/>
    
        </android.support.design.widget.NavigationView>
    
    
    
    2。在@menu/activity\u main\u drawer文件中添加一些适合页眉高度的空项

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:enabled="false"
              android:title=" " />
        <item android:enabled="false"
              android:title=" " />
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/home"
                android:title="@string/home" />
            ......
        </group>
    </menu>        
    
    
    ......
    
    嘿,伙计们,我想出了一个导航抽屉的解决方案,以及如何在不编写任何额外代码的情况下实现静态导航抽屉

    我正在分享我目前正在进行的项目的git链接。 谢谢 下面是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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_main_bg_shape"
            android:orientation="vertical">
    
            <Button
                android:id="@+id/whats_cool"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="260dp"
                android:layout_marginTop="400dp"
                android:background="@drawable/buttonshape"
                android:fontFamily="@font/delius_unicase"
                android:shadowColor="#362802"
                android:shadowDx="4"
                android:shadowDy="3"
                android:shadowRadius="10"
                android:text="What's Cool"
                android:textColor="#003931"
                android:textSize="12sp" />
    
        </LinearLayout>
    
    
        <android.support.design.widget.NavigationView
            app:headerLayout="@layout/nav_header"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/gray_white"
            app:menu="@menu/nav_drawer"
            android:layout_gravity="start"
            app:itemTextColor="@color/darkBlue"
            app:itemIconTint="@color/darkBlue"
            >
        <include layout="@layout/nav_header"/>
    
        </android.support.design.widget.NavigationView>
    
    </android.support.v4.widget.DrawerLayout>
    
    
    

    您不需要对标题布局和导航抽屉进行任何更改。

    NavigationView
    只是一个
    RecyclerView
    ,顶部的视图将与其余视图一起滚动。如果您不想这样做,您必须创建自己的布局以用作菜单。感谢您的回复。所以我的理解是,我必须创建一个自定义导航视图,而不是支持设计小部件,是吗?不需要是自定义视图。例如,您可以添加一个带有标题和导航视图的线性布局,以找到解决方法。绝对不是最有效的。请建议是否可以从这里做些什么。android:id=“@+id/nav\u view”android:layout\u width=“wrap\u content”android:layout\u height=“match\u parent”android:layout\u gravity=“start”android:fitsystemwindows=“true”android:background=“#00000000”app:headerLayout=“@layout/nav\u header\u main”app:menu=“@menu/activity\u main\u drawer”>检查我的答案是否有标题布局冗余,因为标题布局与菜单重叠,因此菜单的某些部分隐藏在不重要的解决方法下面。它对我有用。谢谢Deven。这与下面“抛物线”(导致冗余)中的答案相同,在你的帖子下方用“Deven”来回答。