如何阻止Android导航抽屉自动打开? [英] How to stop Android navigation drawer from opening automatically?
问题描述
如何阻止Android应用程序中的导航抽屉自动打开?
How to stop the navigation drawer in my Android app from opening automatically?
它过去工作正常.起初不可见,并且可以进入可视范围.但是我需要一个标题.起初它只是一个ListView.修改抽屉xml(参见下文)以使其在列表视图上方具有标题后不久,它开始自动打开.我当然没有在代码中添加任何内容,例如my_nav_drawer.openOnStartup().
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" />
最新新闻:
我已经证实上面的XML是原因.我只是将导航抽屉的XML恢复为ListView本身...
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" />
...,它将不再自动打开.外观和工作正常.我的目标是添加一个TextView标题,而不会在启动时打开抽屉.
... 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.
推荐答案
并不是您的抽屉正在自动打开.这是因为DrawerLayout
找不到要用作抽屉的View
,所以它的两个直接子View
都在填充它. LinearLayout
应该是最后列出的抽屉,它覆盖了内容View
,因此抽屉似乎是打开的.
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 View
s 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
通过在其直接子级中查找具有水平layout_gravity
设置的抽屉来确定将哪个View
用作抽屉.即left
/right
或start
/end
.您希望在LinearLayout
上设置此属性,因为它现在充当抽屉.在主布局中,只需将android:layout_gravity="left"
从ListView
移到LinearLayout
.
DrawerLayout
determines which View
s 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
.
这篇关于如何阻止Android导航抽屉自动打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!