Android导航视图:仅滚动菜单项问题

时间:2015-06-19 11:01:09

标签: android android-layout android-studio slidingdrawer android-navigation

我只想滚动导航视图的菜单项,但它也会用它滚动标题。请任何人告诉我如何实现这一点......这是我的代码:

activity_main.xml中

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        .......    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawer" />
    </android.support.v4.widget.DrawerLayout>

header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:background="@drawable/background_material"
        android:orientation="vertical"
        >

        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="76dp"
            android:layout_height="76dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:src="@mipmap/ic_launcher"
            app:border_color="#FF000000" /
    </RelativeLayout>

drawer_menu_items.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/inbox"
            android:checked="false"
            android:icon="@drawable/ic_inbox_black"
            android:title="@string/inbox_string" />

        <item
            android:id="@+id/starred"
            android:checked="false"
            android:icon="@drawable/ic_star_black"
            android:title="@string/starred_string" />

        <item
            android:id="@+id/sent_mail"
            android:checked="false"
            android:icon="@drawable/ic_send_black"
            android:title="@string/sent_mail_string" />

        <item
            android:id="@+id/drafts"
            android:checked="false"
            android:icon="@drawable/ic_drafts_black"
            android:title="@string/draft_string" />


        <item
            android:id="@+id/allmail"
            android:checked="false"
            android:icon="@drawable/ic_email_black"
            android:title="@string/all_mail_string" />
        <item
            android:id="@+id/trash"
            android:checked="false"
            android:icon="@drawable/ic_delete_black"
            android:title="@string/trash_string" />
        <item
            android:id="@+id/spam"
            android:checked="false"
            android:icon="@drawable/ic_error_black"
            android:title="@string/spam_string" />
        <item
            android:id="@+id/abc"
            android:checked="false"
            android:icon="@android:drawable/ic_menu_camera"
            android:title="Camera" />
        <item
            android:id="@+id/abcd"
            android:checked="false"
            android:icon="@android:drawable/ic_menu_call"
            android:title="Call" />
        <item
            android:id="@+id/abcde"
            android:checked="false"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="Gallery" />

        <item

            android:checked="false"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item

            android:checked="false"
            android:icon="@android:drawable/ic_menu_compass"
            android:title="Compass" />
        <item

            android:checked="false"
            android:icon="@android:drawable/ic_menu_myplaces"
            android:title="My Places" />

    </group>
</menu>

我怎样才能滚动菜单项而不是标题...我还附加了一些图片... Before Scrolling After Scrolling

5 个答案:

答案 0 :(得分:4)

您应该可以将标题放在NavigationView之外,如下所示:

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

    <!-- nav drawer -->
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        >
        <!-- header -->
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="190dp"
            android:background="@drawable/background_material"
            android:orientation="vertical"
            >

            <de.hdodenhof.circleimageview.CircleImageView 
                android:id="@+id/profile_image"
                android:layout_width="76dp"
                android:layout_height="76dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="24dp"
                android:layout_marginStart="24dp"
                android:src="@mipmap/ic_launcher"
                app:border_color="#FF000000" />
        </RelativeLayout>

        <!-- menu -->
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="0px"
            android:layout_weight="1"
            android:layout_gravity="start"
            app:menu="@menu/drawer" />
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

注意:我添加了FrameLayout来封装DrawerLayout的单个视图中的所有内容,调整NavigationView的高度以自动使用标题下方的可用空间,并删除了{{ 1}}属性。

答案 1 :(得分:1)

我知道它的问题太老了,但是太简单了!

<android.support.design.widget.NavigationView
    android:id="@+id/navigationview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header_drawer"
    app:menu="@menu/item_drawer">

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

将以下代码添加到NavigationView(<include layout="@layout/header_drawer"/>

<android.support.design.widget.NavigationView
     android:id="@+id/navigationview"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     app:headerLayout="@layout/header_drawer"
     app:menu="@menu/item_drawer">

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

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

答案 2 :(得分:0)

我也遇到了这个问题并解决了它。这是解决方案

1)在项目中创建默认导航抽屉

2)从activity_main.xml中的NavigationView中删除标头标签,并在NavigationView

中添加标头
 <div class="form-group">
        @Html.LabelFor(model => model.Postnr, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Postnr, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Postnr, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.By, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.By, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.By, "", new { @class = "text-danger" })
        </div>
    </div>

3)在菜单顶部添加所需的空白项目&gt; activity_main_drawer.xml(标题高度上的depand

答案 3 :(得分:0)

我通过

解决了这个问题
<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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

//where your main content goes.
<include
    layout="@layout/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<include
    layout="@layout/nav_drawer_layout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true" />

内部抽屉布局,包括添加此布局,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

 //your header layout.
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:gravity="bottom"
    android:orientation="vertical">

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

</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/drawerRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_weight="1"
    android:background="#ffffff" />

   //your footer layout
<include layout="@layout/nav_footer_item" />

然后只填充recyclerview。

答案 4 :(得分:-1)

我知道它太古老的问题,但它太容易了。

<android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawer" />
    </android.support.v4.widget.DrawerLayout>

将其更改为代码 (include header inside)

 <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer" />

              <include layout="@layout/header"/>
    </android.support.v4.widget.DrawerLayout>

魅力快乐编码一样工作:)