如何添加页脚到 NavigationView-Android 支持设计库?

如何将页脚设置和配置文件项设置为 NavitationView?看起来像电子邮件导航抽屉的收件箱。NavitationView项目是由菜单资源膨胀,但我不知道如何设置菜单资源的底部项目,或如何设置一个自定义视图的 NavigationView或底部偏移量?我尝试把这个 <LinearLayout...>作为页脚视图,但在小屏幕上页脚放在项目上,我不能滚动菜单,我试图设置一个页脚填充到 NavigationView,但页脚也需要填充。

这不是在小屏幕上滚动:

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


<LinearLayout...>


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

NOT SCROLLING

这个页面会滚动,但页脚位于菜单项之上:

<android.support.design.widget.NavigationView
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingBottom="96dp"
app:headerLayout="@layout/kuona_drawer_header"
app:menu="@menu/drawer">


<LinearLayout...>


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

enter image description here

抽屉菜单 res/menu/drawer.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/action_current_list"
android:checked="true"
android:icon="@drawable/ic_current_list"
android:title="@string/current_list" />
<item
android:id="@+id/action_manage_lists"
android:icon="@drawable/ic_my_lists"
android:title="@string/my_lists" />
<item
android:id="@+id/action_search_products"
android:icon="@drawable/ic_search_black_24dp"
android:title="@string/search_products" />
<item
android:id="@+id/action_deals"
android:icon="@drawable/ic_product_promo"
android:title="@string/deals" />
</group>
</menu>
89114 次浏览

按照您的方法,一些小的变化可以帮助您实现您想要实现的目标。

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/background_material_light">
<TextView
android:id="@+id/footer_item"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:text="Something"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>

并在菜单中设置一些存根项,这样菜单项就不会重叠。

<group>
...
<item
android:title=""
android:orderInCategory="200"/>
</group>

此外,您还需要向页脚项添加一个单击侦听器。

如果您想在导航菜单中有一个固定的(不滚动的)页脚,那么您需要将 NavigationView 包裹在另一个布局中,就像您已经发布的那样。NavigationView 的工作原理与 FrameLayout 类似,所以这样就会在 NavigationView 菜单项的顶部“堆叠”内部布局。这里有一种排列方法,对页脚项使用 LinearLayout:

固定页脚

<android.support.design.widget.NavigationView
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/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/footer_item_1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="Footer Item 1" />
<TextView
android:id="@+id/footer_item_2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="Footer Item 2" />
</LinearLayout>


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

我在这个例子中使用了 TextView,但是您可以对页脚视图使用任何您想要的东西。为了避免页脚项与菜单底部重叠,可以在菜单资源文件的末尾添加一些虚拟项(这些项的作用类似于“间隔项”) :

Res/menu/drader.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:id="@+id/nav_item_1"
android:icon="@drawable/ic_nav_item_1"
android:title="Nav Item 1" />
<item
android:id="@+id/nav_item_2"
android:icon="@drawable/ic_nav_item_2"
android:title="Nav Item 2" />
<item
android:id="@+id/nav_item_3"
android:icon="@drawable/ic_nav_item_3"
android:title="Nav Item 3" />
<item
android:id="@+id/nav_item_4"
android:icon="@drawable/ic_nav_item_4"
android:title="Nav Item 4" />
<item
android:id="@+id/footer_spacer_1"
android:checkable="false"
android:enabled="false"
android:orderInCategory="200"
android:title="" />
<item
android:id="@+id/footer_spacer_2"
android:checkable="false"
android:enabled="false"
android:orderInCategory="200"
android:title="" />
</group>
</menu>

最后,不要忘记在你的活动中为实际的页脚视图添加点击侦听器:

...
// Click listener for nav footer.
View navFooter1 = findViewById(R.id.footer_item_1);
navFooter1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do footer action
}
});
View navFooter2 = findViewById(R.id.footer_item_2);
navFooter2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do footer action
}
});
...

滚动页脚

不过,如果允许页脚与 NavigationView 的其余部分一起滚动,那么事情就会变得更简单(不需要额外的布局或单击侦听器)。只需将页脚项目作为唯一的 <group>添加到菜单资源文件中(这将创建一个 分离线) ,所有东西都会自动处理并一起滚动:

Res/menu/drader.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/nav_menu">
<item
android:id="@+id/nav_item_1"
android:icon="@drawable/ic_nav_item_1"
android:title="Nav Item 1" />
<item
android:id="@+id/nav_item_2"
android:icon="@drawable/ic_nav_item_2"
android:title="Nav Item 2" />
<item
android:id="@+id/nav_item_3"
android:icon="@drawable/ic_nav_item_3"
android:title="Nav Item 3" />
<item
android:id="@+id/nav_item_4"
android:icon="@drawable/ic_nav_item_4"
android:title="Nav Item 4" />
</group>
<group android:id="@+id/nav_footer">
<item
android:id="@+id/nav_footer_1"
android:icon="@drawable/ic_footer_item_1"
android:title="Footer Item 1" />
<item
android:id="@+id/nav_footer_2"
android:icon="@drawable/ic_footer_item_2"
android:title="Footer Item 2" />
</group>
</menu>

我只是给你提示如何解决这个问题,但我没有机会在 NavigationView 上测试它,并且非常肯定它会工作

这里的示例布局 xml;

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="96dp">


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6F00" />


<TextView
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_gravity="bottom"
android:layout_marginBottom="-96dp"
android:background="#600F" />


</FrameLayout>

结果是这样的:

enter image description here

诀窍是对父级应用填充,对子级应用负边距。


快速尝试:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clipToPadding="false"
android:paddingBottom="96dp"
app:headerLayout="@layout/sample_header"
app:menu="@menu/sample_menu">




<TextView
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_gravity="bottom"
android:layout_marginBottom="-96dp"
android:background="#600F"
android:gravity="center"
android:text="I STAND BY MY SELF" />


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

enter image description here

您需要有一个容器导航视图布局,然后它应该包含另外两个导航布局。您可以将它们对齐到父布局的顶部和底部。

我建议使用导航视图作为父视图,而不是 FrameLayout,因为它本质上是一个 ScrimFrameLayout,与状态栏的交互更好。

下面是一个你的活动应该是什么样子的例子:

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

您可以阅读有关它的更多信息,并在这里看到一个示例: http://blog.nitish.io/post/122633295558/android-design-library-navigationview-with-top

只需在导航视图中添加另一个布局:

<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#000000"
app:itemTextColor="#FFFFFF"
app:headerLayout="@layout/fragment_side_menu_header"
app:menu="@menu/side_menu">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom">
<TextView
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" />
<TextView
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test2" />
</LinearLayout>
</android.support.design.widget.NavigationView>

诀窍在于使用 lay _ vity = “ bottom”——这将把整个布局放在底部,并且 test2被正确地堆叠起来。

第一个子级 NavigationView是同时包含头部和菜单项的 ListView

添加页脚所需的惟一操作是将.addFooterView 调用到 ListView

更多信息: http://www.andreabaccega.com/blog/2015/08/28/how-to-add-footer-to-navigationview/

复制粘贴代码:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


ListView listView = (ListView) navigationView.getChildAt(0);
View toRet = LayoutInflater.from(view.getContext()).inflate(R.layout.drawer_footer, listView, false);


// Manipulate the view (if you need to) before calling addFooterView.


listView.addFooterView(toRet, null, false);
}

我的解决方案与固定的页脚和滚动菜单(100% 测试)

 <android.support.design.widget.NavigationView
android:id="@+id/container_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity=""
android:nestedScrollingEnabled="true"
android:scrollIndicators="none">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@+id/navigation2"
android:layout_gravity="top"
android:nestedScrollingEnabled="true"
android:paddingBottom="@dimen/dimen_20_dp"
app:headerLayout="@layout/nav_header"
app:itemIconTint="@color/black_800"
app:itemTextColor="@color/black_800"
app:menu="@menu/navigation_drawer_items">


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


<android.support.design.widget.NavigationView
android:id="@+id/navigation2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/white_100"
android:orientation="horizontal">


<TextView
android:id="@+id/empty_spacer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/ic_search"
android:gravity="center"
android:text="Share" />


<TextView
android:id="@+id/mnuRate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/ic_search"
android:gravity="center"
android:text="Rate" />


<TextView
android:id="@+id/mnuHelp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/ic_search"
android:gravity="center"
android:text="Help" />
</LinearLayout>
</android.support.design.widget.NavigationView>


</RelativeLayout>


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

用这个。

<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:itemIconTint="@color/accent"
app:itemTextColor="@color/primary_text"
app:menu="@menu/navigation_drawer_items">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/grey_200"
android:orientation="vertical">


<View
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:background="@color/grey_600"/>


<com.facebook.share.widget.LikeView
android:id="@+id/like_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:padding="@dimen/small"/>


<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/small"/>
</LinearLayout>
</android.support.design.widget.NavigationView>

然后将底部填充设置为 NavigationMenuView

final View menuView = navigationView.getChildAt(0);
final View bottomView = navigationView.getChildAt(1);
bottomView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
menuView.setPadding(0, 0, 0, bottomView.getMeasuredHeight());
}
});

我使用这种形式,为我工作。在风景 & 肖像。

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


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
app:headerLayout="@layout/master_main_header"
app:itemIconTint="@color/blue"
app:menu="@menu/menu_drawer">


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


<Button
android:id="@+id/master_btn_closession"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@color/blue"
android:text="Cerrar sesión" />
</LinearLayout>
</android.support.design.widget.NavigationView>

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"
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_base"
android:layout_width="match_parent"
android:layout_height="match_parent" />


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


<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:isScrollContainer="true"
app:headerLayout="@layout/nav_header_base"
app:menu="@menu/activity_base_drawer"
android:layout_gravity="top"
android:layout_marginBottom="x"
/>


<android.support.design.widget.NavigationView
android:id="@+id/nav_view_footer"
android:layout_width="wrap_content"
android:layout_height="x"
app:headerLayout="@layout/hear_layout"
app:menu="@menu/menu_items"
android:scrollbars="none"
android:layout_gravity="bottom"
/>


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


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

如果你的页脚是一个列表,

    app:headerLayout="@null"
app:menu="@menu/activity_base_drawer_footer"

但是,如果这是某种定制的观点,

    app:headerLayout="@layout/my_cutom_footer_view"
app:menu="@null"

此外,在这种情况下,您将需要设置 x = height of your custom footer view

希望能有帮助。

按照嵌套导航视图的其他答案中描述的方法,出现了一些问题:

  • 对于许多项目,或者在横向模式下,页脚与菜单项重叠
  • 如果真正的菜单有很多项,那么嵌套的 NavigationView 可以滚动,这看起来不太好
  • 在嵌套中有两个 NavigationView,不允许将自定义视图定义为页脚。
  • 嵌套的滚动视图的处理非常混乱(有时会出现两个滚动条等)
  • 固定页脚应该总是在底部(很少以及很多菜单项)

我对所有这些问题的解决办法如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>


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


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


<android.support.v4.widget.NestedScrollView
...
android:fillViewport="true"
android:scrollbars="vertical">


<LinearLayout
...
android:orientation="vertical">


<android.support.design.widget.NavigationView
...
app:elevation="0dp"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu">
</android.support.design.widget.NavigationView>


<LinearLayout
android:id="@+id/spacer_to_bottom"
...
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>


<include layout="@layout/nav_footer"></include>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

在这里,NestedScrollView 充当子 NavigationView 的滚动父级。 这意味着,子 NavigationView 本身不会显示滚动条,而是以平面的方式显示整个内容。

布局‘ space _ to _ bottom’填充了所有剩余的空间,因此只有很少的菜单图标,页脚仍然在底部。

最后,将固定的页脚添加到线性布局中,该布局从真正的菜单(子 NavigationView)、间隔符开始,并在底部具有页脚。

在这里您可以找到完整的工作示例 AndroidStudio-Project: https://github.com/MarcDahlem/AndroidSidemenuFooterExample

特别是导航抽屉可以在这里找到: Https://github.com/marcdahlem/androidsidemenufooterexample/blob/master/app/src/main/res/layout/activity_main.xml

截图:

Few itemsMany items

最简单的答案是在 Drawer 布局中添加一个按钮,并将其重力设置为 navigationview.xml的底部。

密码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/menu_navigation">
   

<Button
android:id="@+id/btn_sing_in"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/sign_in"
android:layout_gravity="bottom"/>


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

试试这个,这个对我有用。

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


<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">


<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_header_admin"
app:menu="@menu/activity_admin_drawer"/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/lyNavFooter">


<!--INCLUDE YOUR FOOTER HERE -->


</LinearLayout>
</LinearLayout>


</ScrollView>






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

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />


<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer">


<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
></android.support.design.widget.NavigationView>


<LinearLayout
android:id="@+id/spacer_to_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />


</LinearLayout>
</android.support.v4.widget.NestedScrollView>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp">


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


</LinearLayout>
</android.support.design.widget.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"
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_main3"
android:layout_width="match_parent"
android:layout_height="match_parent" />


<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f00"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main3_drawer">


<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent"
android:scrollbars="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
app:elevation="0dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#ff0"
app:headerLayout="@layout/nav_header_main3"
app:menu="@menu/activity_main3_drawer">
></android.support.design.widget.NavigationView>


<LinearLayout
android:id="@+id/spacer_to_bottom"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#0f0"
android:layout_height="0dp"
android:layout_weight="1">
<include layout="@layout/nav_footer_main3"></include>
</LinearLayout>




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


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

我的导航 _ footer _ main3是

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/logo_1" />
</LinearLayout>

为 Version > 23.x. x 滚动页脚

我最终设法实现了我想要的,不幸的是,看起来不再可能只是抓取 ListView 的引用并添加一个页眉和页脚,如23.x.x 以下版本(如 Andrea Baccega 所描述的)。对头部执行此操作仍然是可能的:

     <android.support.design.widget.NavigationView
..
app:headerLayout="@layout/item_drawer_footer"
..
/>

但目前不可能添加页脚。然而,我发现了一个变通方法,以防你只是想添加页脚: 你只需要反转视图,这将添加页眉到底部,其行为就像一个正常的页脚。只要确保按相反的顺序创建菜单即可

    // Grab reference to the embedded recycler view
RecyclerView mRecyclerView = (RecyclerView) navigationView.getChildAt(0);


// Create a LinearLayoutManager and set it to reversed
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);


// Apply layout manager to the recycler view
mRecyclerView.setLayoutManager(mLayoutManager);

这就是我如何实现在导航底部添加布局:

更新的库

    <com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_drawer_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">


<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="0.8"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />


<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_drawer_bottom"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2">


<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/scrollView"
android:orientation="vertical">


<TextView
android:id="@+id/text_dashboard_followUsAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:text="Follow us at" />


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingStart="16dp">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/fb" />


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/fb" />


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/fb" />
</LinearLayout>


<TextView
android:id="@+id/text_dashboard_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="25dp"
android:paddingBottom="5dp"
android:paddingEnd="16dp"
android:paddingRight="16dp"
android:text="Version 1.0" />
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</com.google.android.material.navigation.NavigationView>

我个人的固定页眉和页脚解决方案是扩展 NavigationView 如下:

/**
* Created by guness on 17.01.2018.
*/
class NavigationView : android.support.design.widget.NavigationView {


private var mHeader: View? = null
private var mFooter: View? = null
private var mMenuView: NavigationMenuView? = null


constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
val a = TintTypedArray.obtainStyledAttributes(context, attrs,
R.styleable.NavigationView, defStyleAttr,
R.style.Widget_Design_NavigationView)


if (a.hasValue(R.styleable.NavigationView_footerLayout)) {
inflateFooterView(a.getResourceId(R.styleable.NavigationView_footerLayout, 0))
}


a.recycle()


(mFooter?.layoutParams as FrameLayout.LayoutParams?)?.gravity = Gravity.BOTTOM
}


init {
(0 until childCount)
.map { getChildAt(it) }
.filter { it is NavigationMenuView }
.forEach {
mMenuView = it as NavigationMenuView
mMenuView!!.overScrollMode = View.OVER_SCROLL_NEVER
}
}


override fun inflateHeaderView(@LayoutRes res: Int): View {
mHeader = LayoutInflater.from(context).inflate(res, this, false)
setHeaderView(mHeader!!)
return mHeader!!
}


@Deprecated("There can only be one header", ReplaceWith("#setHeaderView(view: View)"))
override fun addHeaderView(view: View) {
throw IllegalAccessException("Please use #setHeaderView")
}


@UiThread
fun setHeaderView(view: View) {
removeHeaderView()
mHeader = view
addView(mHeader, 0)
}


@Deprecated("No need to use params", ReplaceWith("#removeHeaderView()"))
override fun removeHeaderView(view: View) {
removeHeaderView()
}


@UiThread
fun removeHeaderView() {
if (mHeader != null) {
removeView(mHeader)
mHeader = null
}
}


@Deprecated("No need to count, it is either 1 or zero", ReplaceWith("#hasHeader()"))
override fun getHeaderCount(): Int {
return if (mHeader == null) 0 else 1
}


@Deprecated("No need to use params", ReplaceWith("#getHeaderView()"))
override fun getHeaderView(index: Int): View? {
return getHeaderView()
}


fun getHeaderView(): View? {
return mHeader
}


fun hasHeader(): Boolean {
return mHeader != null
}


fun inflateFooterView(@LayoutRes res: Int): View {
mFooter = LayoutInflater.from(context).inflate(res, this, false)
setFooterView(mFooter!!)
return mFooter!!
}


@UiThread
fun setFooterView(view: View) {
removeFooterView()
mFooter = view
addView(mFooter, 0)
}


@UiThread
fun removeFooterView() {
if (mFooter != null) {
removeView(mFooter)
mFooter = null
}
}


fun hasFooter(): Boolean {
return mFooter != null
}


fun getFooterView(): View? {
return mFooter
}


fun setOnClickListener(@IdRes res: Int, listener: View.OnClickListener) {
mHeader?.findViewById<View>(res)?.setOnClickListener(listener)
mFooter?.findViewById<View>(res)?.setOnClickListener(listener)
}


override fun onMeasure(widthSpec: Int, heightSpec: Int) {
super.onMeasure(widthSpec, heightSpec)
val headerHeight = mHeader?.measuredHeight ?: 0
val footerHeight = mFooter?.measuredHeight ?: 0
val params = (mMenuView?.layoutParams as ViewGroup.MarginLayoutParams?)
var changed = false
if (params?.topMargin != headerHeight) {
params?.topMargin = headerHeight
changed = true
}
if (params?.bottomMargin != footerHeight) {
params?.bottomMargin = footerHeight
changed = true
}
if (changed) {
mMenuView!!.measure(widthSpec, heightSpec)
}
}
}

最初,NavigationView 创建一个 LinearLayout 作为回收视图上的第一个项,并将所有内容一起滚动。这个想法是为页脚和页眉创建单独的视图,然后使用 Gravity 将它们推到顶部和底部。稍后,在测量侯赛因视图的内容之后,确定滚动内容。

下面是包含我写的上述代码的库。 Https://github.com/guness/navigationview

好的一面是,现在我可以在 xml 上定义 footer 视图,就像在本机上定义头文件一样:

    app:footerLayout="@layout/nav_footer_main"
app:headerLayout="@layout/nav_header_main"

抽屉菜单中粘贴页眉和页脚的布局结构:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout>


<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>


<LinearLayout>
<include layout="@layout/drawer_header"/>
<android.support.design.widget.NavigationView/>
<include layout="@layout/drawer_footer"/>
</LinearLayout>


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

完整的布局:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">


<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:layout_gravity="start"
android:orientation="vertical">
<include layout="@layout/drawer_menu_header"/>


<android.support.design.widget.NavigationView
android:id="@+id/drawer_menu_body"
app:elevation="0dp"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:theme="@style/AppTheme.PopupOverlay"
app:menu="@menu/main_drawer">
</android.support.design.widget.NavigationView>


<include layout="@layout/drawer_menu_footer"/>
</LinearLayout>


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

试试这个,这个对我有用

但是,您是否禁用了 NavigationViewScroll 以实现更平滑的滚动

private void disableNavigationViewScrolling(NavigationView navigationView) {
if (navigationView != null) {
NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
if (navigationMenuView != null) {
navigationMenuView.setNestedScrollingEnabled(false);
}
}
}

截图:

enter image description here我用以下方式做了同样的事情:

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


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


<LinearLayout android:layout_gravity="bottom"
android:background="#20191d1e"
android:layout_width="match_parent"
android:paddingBottom="2dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="2dp"
android:orientation="horizontal"
android:layout_height="wrap_content">


<ImageView
android:id="@+id/company_image_id"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="@dimen/margin1dp"
android:padding="@dimen/margin2dp"
android:src="@mipmap/ic_launcher_round"
/>


<TextView
android:id="@+id/txtCompanyName"
android:layout_width="match_parent"                              android:layout_marginLeft="@dimen/margin3dp"
android:layout_height="wrap_content"
android:textSize="13dp" android:layout_gravity="center"
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
</android.support.design.widget.NavigationView>


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

这里主要是我把布局重力放在底部。

LinearLayout android:layout_gravity="bottom"

我知道它的后期答案,但它的完美和准确的答案,大多数开发人员寻找。

要在导航视图中添加页脚,请在导航菜单中添加自定义视图,如下所示:

Footer _ 導航 _ menu. xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:orientation="horizontal">


<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/version" />


<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right" />


</RelativeLayout>

现在,将上面的视图添加到具有 group 属性的菜单 xml 中。

Profile _ menu. xml

<group android:checkableBehavior="single">


<item
android:id="@+id/nav_support"
android:title="@string/nav_item_support" />


<item
android:id="@+id/nav_settings"
android:title="@string/nav_item_settings" />


<item
android:id="@+id/nav_log_out"
android:title="@string/nav_item_log_out" />
</group>
<group
android:id="@+id/nav_footer">
<item
android:id="@+id/nav_log_version"
app:actionLayout="@layout/footer_navigation_menu" />
</group>

就是这样。下面是输出:

enter image description here

这里有一个不需要嵌套的解决方案,动态的根据页脚视图自身的高度调整内部 NavigationMenuView 的底部填充,这意味着可以将页脚高度设置为 wrap_content,并且可以动态地将页脚的可见性更改为 VISIBLEGONE

public class NavigationMenuFooterView extends LinearLayout {


public NavigationMenuFooterView(Context context) {
super(context);
}


public NavigationMenuFooterView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}


public NavigationMenuFooterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
update(getHeight());
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
update(h);
}


private void update(int height) {


ViewParent parent = getParent();
if (parent instanceof ViewGroup) {
View navigationMenuView = ((ViewGroup) parent).findViewById(R.id.design_navigation_view);
if (navigationMenuView != null) {
if (getVisibility() == View.GONE) {
height = 0;
}
navigationMenuView.setPadding(navigationMenuView.getPaddingLeft(),
navigationMenuView.getPaddingTop(), navigationMenuView.getPaddingRight(), height);
}
}
}
}

您需要在 ids.xml 文件中定义 design _ 導 _ view 的 id:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="design_navigation_view" type="id"/>
</resources>

像这样使用:

    <com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemBackground="@drawable/drawer_item"
app:itemIconTint="@color/drawer_item"
app:itemTextColor="@color/drawer_item"
app:menu="@menu/menu_drawer">


<util.NavigationMenuFooterView
android:id="@+id/navigation_footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#fff"
android:orientation="vertical">


<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ddd" />


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


</util.NavigationMenuFooterView>


</com.google.android.material.navigation.NavigationView>

com.google.android.material:material:1.0.0测试。

把这些行放进你的菜单布局,我希望它能解决你的问题:

    <group
android:id="@+id/grp11"
android:checkableBehavior="single">


<item




android:title="" />
<item




android:title="" />
<item


android:title="" />
<item




android:title="" />
<item




android:title="" />
<item


android:title="" />


</group>

如果您正在使用 listview,您可以尝试这种方法。列表视图支持添加页脚视图功能,因此您可以使用 listview 对象添加自定义页脚 R.layout.pull _ footer 视图,如下面的代码所示

View footerView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.drawer_footer, expandableListView, false);
TextView footer = footerView.findViewById(R.id.id_footer_wta_version_information);
expandableListView.addFooterView(footerView);

在我的例子中,我使用 expandableListView 代替 listview (listview 也有 addFooter 函数)

对于我的情况下,我需要保持在“导航视图”底部的“注销”项总是如果有足够的空间或“模拟”一些滚动,如果不适合。

我们的目标是在不做大的更改,当然也不添加嵌套的 NavigationView 的情况下实现这一目标。

对我有效的解决办法如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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">


<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clipToPadding="false"
android:clipChildren="false"
android:overScrollMode="never"
android:fitsSystemWindows="true"
android:maxWidth="300dp"
app:headerLayout="@layout/nav_drawer_header"
app:itemBackground="@drawable/bg_nav_drawer_item"
app:itemHorizontalPadding="25dp"
app:itemIconPadding="25dp"
app:itemIconTint="@color/nav_drawer_item_icon"
app:itemTextAppearance="@style/NavigationDrawerTextStyle"
app:itemTextColor="@color/nav_drawer_item_text"
app:menu="@menu/navigation_drawer_menu">


<LinearLayout
android:id="@+id/ll_activity_main_log_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?attr/selectableItemBackground"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
android:paddingStart="30dp"
tools:ignore="RtlSymmetry">


<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_log_out"
android:tint="@color/gray_B3BAC2" />


<TextView
style="@style/TextBodyBold.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingVertical="20dp"
android:paddingStart="25dp"
android:paddingEnd="50dp"
android:text="@string/label_logout"
android:textColor="@color/gray_B3BAC2" />
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>


MainActivity.kt

class MainActivity : AppCompatActivity() {


private lateinit var mainActivityBinding: ActivityMainBinding
// ...
    

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)


mainActivityBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
mainActivityBinding.apply {
lifecycleOwner = this@MainActivity
llActivityMainLogOut.setOnClickListener { mainViewModel.handleOnLogoutUserPress() }
}
}


private var onScrollListener: RecyclerView.OnScrollListener? = null


/**
* Listener for "Logout" Drawer Menu Item that checks if should
* add a scrollListener (and update bottom margin) or keep the item at the bottom of the view.
*/
private val navigationViewGlobalLayoutListener = object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Get the NavigationView RecyclerView List Menu
val drawerViewList = mainActivityBinding.navigationView.getChildAt(0) as RecyclerView


if (onScrollListener == null) {
val scrollExtent = drawerViewList.computeVerticalScrollExtent()
val scrollRange = drawerViewList.computeVerticalScrollRange()
// Check if the list has enough height space for all items
if (scrollExtent < scrollRange) {
// Adding NavigationView Bottom padding. Is where the logoutItem goes
mainActivityBinding.navigationView.setPadding(
0,
0,
0,
resources.getDimensionPixelSize(R.dimen.drawer_layout_footer_padding_bottom)
)


// The first item of the list is a Header, getting the second menu item height
val itemHeight = drawerViewList.getChildAt(1).measuredHeight
// The first update will add enough negative bottom margin and will hide the item
updateDrawerLogoutItemBottomMargin(scrollExtent, scrollRange, itemHeight)


onScrollListener = object : RecyclerView.OnScrollListener() {
private var totalScrollY = 0


override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
totalScrollY += dy


updateDrawerLogoutItemBottomMargin(
recyclerView.computeVerticalScrollExtent(),
recyclerView.computeVerticalScrollRange(),
recyclerView.getChildAt(1).measuredHeight,
totalScrollY
)
}
}


// Adding scroll listener in order to update the bottom logOut item margin
// while the list is being scrolled
drawerViewList.addOnScrollListener(onScrollListener!!)
}
} else {
// The listener have been already added and computed, removing
drawerViewList.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
}
}


/**
* @param scrollExtent the current height of the list
* @param scrollRange the height that needs the list for showing all items
* @param itemHeight the height of a Menu Item (not header)
* @param extraScroll scroll offset performed
*/
private fun updateDrawerLogoutItemBottomMargin(
scrollExtent: Int,
scrollRange: Int,
itemHeight: Int,
extraScroll: Int = 0
) {
mainActivityBinding.llActivityMainLogOut.updateLayoutParams {
this as MarginLayoutParams
bottomMargin = scrollExtent - scrollRange - itemHeight + extraScroll
}
}


override fun onResume() {
super.onResume()
mainActivityBinding.navigationView.viewTreeObserver.addOnGlobalLayoutListener(
navigationViewGlobalLayoutListener
)
}


override fun onPause() {
super.onPause()
if (onScrollListener != null) {
(mainActivityBinding.navigationView.getChildAt(0) as RecyclerView)
.removeOnScrollListener(onScrollListener!!)
}
}


//...
}

activity_main.xml NavigationView中的 android:clipChildren="false"android:clipToPadding="false"是强制参数。

每种情况可能需要不同的底部填充,以添加“额外”抽屉项目,在我的情况下,价值是: <dimen name="drawer_layout_footer_padding_bottom">70dp</dimen>

当然,不要忘记在 onResume方法中注册 on GlobalLayoutListener,如果已经注册,则在 onPause方法中取消注册 scrollListener