简体   繁体   English

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

[英]How to add footer to Navigation Drawer in Android using design support library?

How can I add a footer to a NavigationView ? 如何在NavigationView添加页脚? In my case the NavigationView items are inflated by menu resource. 在我的例子中, NavigationView项目由菜单资源膨胀。 Initially I tried to add a LinearLayout as a footer, but the NavigationView is not scrollable and the footer gets overlapped by the navigation menu items. 最初我尝试将LinearLayout添加为页脚,但NavigationView不可滚动,页脚与导航菜单项重叠。 I want to implement a Google Play rating segment as a footer exactly like this image: 我想将Google Play评分细分作为页脚实现,如下图所示:

在此输入图像描述

navigation_drawer.xml : 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>

It's really simple: 这很简单:

just wrap the desired footer in your NavigationView. 只需在NavigationView中包含所需的页脚。 You can use mine as an example - works like a charm! 你可以用我的例子 - 像魅力一样!

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

You can use any (more or less complex) layout that way. 您可以使用任何(或多或少复杂)布局。 (Afaik this footer won't scroll and will be fixed). (Afaik这个页脚不会滚动并将被修复)。

You can use NavigationView 您可以使用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 http://answermega.livedoor.biz/archives/1038944871.html

https://github.com/mikepenz/MaterialDrawer https://github.com/mikepenz/MaterialDrawer

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

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

And for performing click on it: 并执行点击它:

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();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM