How to stop Android navigation drawer from opening automatically?

旧城冷巷雨未停 提交于 2019-12-29 01:58:05

问题


How to stop the navigation drawer in my Android app from opening automatically?

It used to work fine. Out of sight at first and able to be swiped into visibility. But I needed a title for it. At first it was only a ListView. Soon after modifying the drawer xml (see below) to give it a title above a list view, it began opening automatically. I certainly didn't add anything to the code like my_nav_drawer.openOnStartup().

<!-- The navigation drawer -->
<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">

<!-- The main content view -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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:background="@drawable/ail_background_gradient"
    tools:context="com.allinlearning.assist_android.HomeScreenActivityFragment">

    <ImageView
        android:id="@+id/imgViewLogo"
        android:src="@drawable/ail_logo"
        android:layout_margin="10dp"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:scaleType="fitXY"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="ALL In Learning"
        android:id="@+id/textViewLogo"
        android:layout_margin="10dp"
        android:layout_below="@+id/imgViewLogo"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/font_size26"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/imgBtnGradeAssessment"
        android:src="@drawable/grade_assessment"
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:scaleType="fitXY"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/textViewGradeAssessment"
        android:layout_toStartOf="@+id/textViewGradeAssessment" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Grade"
        android:id="@+id/textViewGradeAssessment"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size28"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/imgViewLogo"
        android:layout_alignEnd="@+id/imgViewLogo" />

    <ImageButton
        android:id="@+id/imgBtnPrivateData"
        android:src="@drawable/two_clickers"
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:scaleType="fitXY"
        android:layout_below="@+id/imgBtnGradeAssessment"
        android:layout_alignLeft="@+id/imgBtnGradeAssessment"
        android:layout_alignStart="@+id/imgBtnGradeAssessment" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Private Data"
        android:id="@+id/textViewPrivateData"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size28"
        android:layout_alignBottom="@+id/imgBtnPrivateData"
        android:layout_toRightOf="@+id/imgBtnPrivateData"
        android:layout_toEndOf="@+id/imgBtnPrivateData"
        android:layout_marginBottom="40dp" />

</RelativeLayout>

<TextView
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="I AM THE TITLE"
    android:id="@+id/tvDrawerTitle"
    android:layout_margin="10dp"
    android:layout_centerHorizontal="true"
    android:textSize="@dimen/font_size26"
    android:textStyle="bold"
    android:textAlignment="center"
    android:textColor="@color/black" />

<ListView
    android:id="@+id/lvDrawerItems"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@color/white" />

Breaking news:

I've verified that in my XML above is the cause. I just reverted the navigation drawer XML back to just the ListView itself ...

<!-- The navigation drawer -->
<ListView
    android:id="@+id/lvDrawerItems"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@color/white" />

... and it no longer opens automatically. Looks and works fine. My goal is to add a TextView title without causing the drawer to open on startup.


回答1:


It's not that your drawer is opening automatically. It's that the DrawerLayout isn't finding a View to use as the drawer, so both of its direct child Views are filling it. The LinearLayout meant to be the drawer, being listed last, is covering the content View, so it just appears that the drawer is open.

DrawerLayout determines which Views to use as drawers by looking through its direct children for ones with a horizontal layout_gravity setting; i.e., left/right, or start/end. You want this attribute set on the LinearLayout, since it's now acting as the drawer. In your main layout, simply move android:layout_gravity="left" from the ListView to the LinearLayout.



来源:https://stackoverflow.com/questions/37757004/how-to-stop-android-navigation-drawer-from-opening-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!