抽屉中的可滚动页脚

时间:2017-09-30 17:18:08

标签: android xml android-layout

我试图在抽屉里添加一个页脚。它工作但它仍然是固定的,它涵盖了一些菜单项。如何将页脚放在列表的末尾(在最后一个元素之后)。它接受来自xml(activity_main_drawer.xml)的菜单项,并且它有一个标题(nav_header_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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/side_nav_bar"
            android:id="@+id/content_main"/>
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_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:menu="@menu/activity_main_drawer">
        <include layout="@layout/nav_footer_main"></include>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

页脚代码(nav_footer_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="@dimen/nav_footer_height"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:background="@drawable/side_nav_bar"

    android:baselineAligned="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:scaleType="centerCrop"
        app:srcCompat="@mipmap/copyright"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/copyright"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/copyright" />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

根据@Adreamus回答问题How to add footer to NavigationView - Android support design library?

您可以通过2个导航视图制作,请查看此应用example on Github

另外,我建议您使用MaterialDrawer Library它会为您节省大量时间

enter image description here

答案 1 :(得分:0)

您可以像这样在导航视图的菜单中将页脚添加为项目 menu_main:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home"
        android:title="@string/home" />
    <item
        android:id="@+id/nav_about"
        android:icon="@drawable/ic_about_me"
        android:title="@string/about" />
</group>
<group>
    <item
        android:id="@+id/nav_footer"
        android:title=""
        app:actionLayout="@layout/nav_footer_main"
        app:showAsAction="never" />
</group>
</menu>

它将是可滚动的,并且也可以从代码中轻松访问,您可以使用以下代码:

val navigationFooter = navView.menu.findItem(R.id.nav_footer).actionView as View
    navigationFooter.findViewById<View>(R.id.btn_call_us).setOnClickListener {
        onCallButtonClicked()
    }

祝你好运!