如何使用设计支持库在Android导航抽屉中添加页脚?

7
如何在NavigationView中添加页脚?在我的情况下,NavigationView的项目是通过菜单资源填充的。最初我尝试将LinearLayout作为页脚添加,但是NavigationView不可滚动,页脚会被导航菜单项目覆盖。我想要实现一个Google Play评分段落作为页脚,就像这张图片一样:enter image description here
navigation_drawer.xml:
<LinearLayout
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:orientation="vertical">

  <android.support.v7.widget.Toolbar
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorPrimary"
      android:id="@+id/toolbar"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      app:title="@string/app_name" />

  <android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:id="@+id/drawerLayout">

    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerView">
    </FrameLayout>

    <android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/shitstuff"
        app:itemTextColor="@color/green"
        app:menu="@menu/drawermenu"
        app:headerLayout="@layout/drawer_header"
        android:layout_marginTop="-24dp">
    </android.support.design.widget.NavigationView>

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

</LinearLayout>
3个回答

13

非常简单:

只需在你的NavigationView中包裹所需的页脚即可。你可以使用我的示例-像魔术一样运作!

 <android.support.design.widget.NavigationView
    android:id="@+id/nvView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:headerLayout="@layout/navigation_header"
    app:itemTextColor="@android:color/black"
    app:menu="@menu/drawer_view">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nav_footer_textview"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"
            android:text="Your Footer Text Here" />

    </LinearLayout>

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

你可以使用任何(更复杂或更简单)的布局方式。

(据我所知,此页脚不会滚动且将被固定在原位。)


10
在小屏幕上(和/或横屏方向),NavigationView 的菜单将会覆盖页脚 - 使用 RelativeLayout 测试过。 - Droidman

3

您可以使用NavigationView

<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/layout_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <!-- Activity content goes here -->

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

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            app:menu="@menu/menu_navigation_drawer" />

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:menu="@menu/menu_navigation_drawer_bottom" />

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

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

http://answermega.livedoor.biz/archives/1038944871.html

https://github.com/mikepenz/MaterialDrawer

https://github.com/rudsonlive/NavigationDrawer-MaterialDesign


这个程序可以运行,但是底部抽屉在顶部抽屉之上,所以你需要向其中添加虚拟项。 - Dan Chaltiel
如何处理滚动条,菜单项现在被隐藏在这个视图后面。 - Ahmad Shahwaiz
如何从内导航视图中删除水平高程? - Bhavin Jadav

0
<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:headerLayout="@layout/nav_header_main"
    app:itemIconTint="@color/colorAccent"
    app:menu="@menu/activity_main_drawer">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nav_footer_textview"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"
            android:text="Your Footer Text Here" />

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

并且要执行点击操作:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
TextView footerTV = (TextView) navigationView.findViewById(R.id.nav_footer_textview);
footerTV.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(MainActivity.this, "Footer is clicked", Toast.LENGTH_SHORT).show();
    }
});

你能否添加一些信息,说明这与其他答案有何不同? - Ben Cox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接