使用 ListView 和 Navigation Drawer 进行 Activity ,不幸的是,它们覆盖了我的 ListView,我无法与它进行“onItemSelected”之类的任何交互。
但是
每当我将 DrawerLayout 更改为不覆盖整个 Activity 而只是 一小块,.
在我的布局代码下面:
<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"
tools:context="marcin.chruscicki.notefication.MainActivity"
android:background="@color/universalBackground_DarkGray">
...
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/notesListView"
android:stackFromBottom="false"
android:layout_alignParentStart="true"
android:layout_above="@+id/versionTxtView" />
<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"
android:fitsSystemWindows="true"
>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#262626" />
</android.support.v4.widget.DrawerLayout>
...
</RelativeLayout>
我无法在任何地方找到答案,我相信这只是一个简单的修复问题。
最佳答案
你的 ListView 不工作的原因是因为你把它放在底层并在它上面放了一个抽屉所以抽屉在 ListView 上并且抽屉设置为 match_parent
<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"
tools:context="marcin.chruscicki.notefication.MainActivity"
android:background="@color/universalBackground_DarkGray">
...
<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"
android:fitsSystemWindows="true"
>
<!-- The main content view -->
<android.support.design.widget.NavigationView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="@+id/nav"/>
</android.support.v4.widget.DrawerLayout>
...
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/notesListView"
android:stackFromBottom="false"
android:layout_alignParentStart="true"
android:layout_above="@+id/versionTxtView" />
</RelativeLayout>
尝试像这样放置 ListView 当您在布局中制作一个项目时,它会一个接一个地覆盖另一个,这意味着您最后放置的项目将在前面 您需要在 fragment 布局中制作抽屉导航 listviw。这是一个完整的示例,您可以如何做 fragment example .希望这对您有所帮助。尝试使抽屉导航的 fragment 变得简单。
关于Android DrawerLayout 覆盖背景ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807162/