如何将导航抽屉与拖杆按钮关联起来?

时间:2016-07-09 19:09:07

标签: android navigation-drawer

我不确定这是否是一个常见的问题,我找不到类似的东西,但也许我只是没有说出正确的名字..

简要说明:

我试图连接到我的导航抽屉,外面有一个按钮(浮动按钮),因此即使抽屉关闭也可以看到按钮,因此拖动按钮会打开抽屉..

真实应用示例:

Waze 这样的应用有:

enter image description here

我的尝试......

所以在我疯狂并设置触控听众和恼人的东西之前,我试着先简单一点..我试着简单地在xml文件的NavigationView中添加按钮..

继承我的acivity_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:clipChildren="false"
    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"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="-80dp"
            android:text="drag me->" />

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

我还确保将此行添加到父级:

android:clipChildren="false"

(因此按钮将能够越过边界..)

这就是它的样子(在预览中):

enter image description here

在实际测试中,按钮确实位于图片中的位置,但如果抽屉完全关闭,按钮会消失..

如果我稍微打开抽屉,按钮就会变得可见..

无论如何,按钮没有响应触摸......我无法通过拖动按钮来关闭抽屉..

那么..我该怎么做才能解决这个问题?我甚至关闭了吗?或者我应该开始诅咒一些触摸听众?

提前致谢!

1 个答案:

答案 0 :(得分:0)

将您的按钮放在导航视图之外

button.setOnClikclistener({


drawer.closeDrawer(GRAVITY) / drawer.openDrawer(GRAVITY)
  }

如果你想让Button沿着Drawer的边缘移动,你可以使用Coordinator布局和东西为Button实现单独的行为

这可以来自Handy https://www.bignerdranch.com/blog/customizing-coordinatorlayouts-behavior/