Android 无法对抽屉布局内的ListView使用布局

Android 无法对抽屉布局内的ListView使用布局,android,listview,navigation-drawer,android-appcompat,layout-gravity,Android,Listview,Navigation Drawer,Android Appcompat,Layout Gravity,我正在尝试在我的应用程序中添加导航抽屉。代码如下: <DrawerLayout 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" tools:context="c

我正在尝试在我的应用程序中添加导航抽屉。代码如下:

<DrawerLayout 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"
    tools:context="com.example.abcxyz.properprojectdb.MyDrawerActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:entries="@array/planets"></ListView>
</DrawerLayout>

我无法为ListView添加属性android:layout\u gravity。我不知道为什么它不能使用(intellisense工作得很好,但它不会显示布局)

我的应用程序在运行时崩溃,但如果我将DroperLayout替换为LinearLayout,则应用程序不会崩溃。 我猜这是因为我没有为ListView添加属性android:layou\u gravity。
这里的快照显示布局重力也不会出现在intellisense中

所有事情似乎都没有问题。您可以将
android:layout\u gravity=“start”
android:layout\u gravity=“end”
放在列表视图中

更新

我看到了我的坏消息@提问者是对的。您必须将
DrawerLayout
更改为
android.support.v4.widget.DrawerLayout
替换:
DrawerLayout
具有
android.support.v4.widget.DrawerLayout

并在build.gradle中添加以下行:

编译“com.android.support:support-v4:22.0.0”

更新


DrawerLayout
应该由两个视图组成,并且您似乎无法将
layout\u gravity
设置为表示抽屉本身的视图。您可以始终将您的
列表视图
包装在其他布局中,然后在
列表视图
上设置
布局

我也遇到了同样的问题,我可能浪费了2个小时来找到解决方案。我尝试重新启动IDE,使其无效,从23降级到22,但没有任何效果

最后,我用硬键盘输入了它,在我完成输入后,它就可以使用了

android:layout_gravity=

(值得一提的是,我在AS 2.1上……可能有一个奇怪的bug)

这是一个新项目。我还没有在MyDrawerActivity.javaI中编写任何代码,我正在使用Android Studio,如果这有帮助的话,但是我的项目中不存在layout_gravity。它是否与appcompat库有关?在我下面的教程(slidenerd)中,他们添加了一个我没有添加的appcompat v4依赖项。可能是因为这个吗?(我已尝试添加它,但可能添加不正确)@sga4如我在回答中所述-您必须使用此支持库中的支持库和抽屉布局,因为它在其他地方不存在哦,抱歉,我忘记了。你在干什么?Android Studio还是Eclipse?我照你说的做了。。当我尝试构建它时,gradle找不到'com.android.support:support-v4:22.0.0',所以我添加了'com.android.support:support-v4:21.+'。当我运行应用程序时,它现在不会崩溃,但是布局重力仍然不会出现在intellisensetrue中!但这很奇怪。当我运行此应用程序时,抽屉会出现,在刷卡时不会滑入/滑出。。即使在文档中,ListView的布局也没有包装在另一个布局中。我不知道它从哪里掉头,但突然,它开始工作了!!不必将ListView包装到另一个布局中,它就可以工作了!我写了布局图,忽略了intellisense,构建了应用程序,它抱怨道。又造了一次,成功了!这是正确的答案,android studio似乎没有自动完成此属性,无论使用哪个版本的v4支持lib,您只需键入“android:layout\u gravity=”就行了。android studio让我们喜欢自动完成。