修复了滚动项目时导航抽屉中的导航标题

Par*_*ola 2 android navigation-drawer android-navigationview

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

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

这是我的activity_main布局文件

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的navigation_header_main布局文件

<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>
Run Code Online (Sandbox Code Playgroud)

PS我是android dev的新手.如果需要,请询问更多资源

Par*_*ola 6

找到了解决方法.绝对不是最有效的.如果可以从这里做任何事情,请建议.

<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>
Run Code Online (Sandbox Code Playgroud)

绝对有效.但标题布局是多余的


Dev*_*ven 5

我知道它的问题太老了,但我分享了一个对我有用的解决方案:

1. 在 NavigationView 中放置 nav_header_main

<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>
Run Code Online (Sandbox Code Playgroud)

2. 在@menu/activity_main_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>        
Run Code Online (Sandbox Code Playgroud)