【问题标题】:Close navigation drawer in Android by tapping on the empty portion of the screen通过点击屏幕的空白部分关闭 Android 中的导航抽屉
【发布时间】:2019-02-04 08:24:18
【问题描述】:

我的 Android 应用中有导航抽屉,其可见性由操作栏中的应用图标控制。但是,我需要能够关闭抽屉,不仅可以通过点击应用程序图标,还可以在用户点击屏幕的空白(未占用)部分时。这是我的主要布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"

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

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"

    android:background="@color/white"
    style="@style/ListViewSeparator"/>

我不明白哪个布局是我需要设置侦听器的布局,因为当我打开导航抽屉时,它包含我的“240dp”列表视图,但屏幕的其余部分变得像“透明黑色”,我我正在寻找屏幕其余部分的“透明黑色”布局

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    找到了:

     mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    

    之前设置为LOCK_MODE_LOCKED_OPENED

    【讨论】:

      【解决方案2】:

      取决于你的兴趣。基于 Google 示例,我使用以下代码实现了相同的功能:

      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ListView) findViewById(R.id.left_drawer);
      ...
      @Override
      public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
          selectItem(position);
      
          mDrawerList.setItemChecked(position, true);
          mDrawerLayout.closeDrawer(mDrawerList);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-15
        • 2014-04-02
        • 2022-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多