【发布时间】:2014-12-08 01:39:18
【问题描述】:
当我点击抽屉开关时,出现以下异常:
java.lang.IllegalArgumentException:没有找到重力的抽屉视图 左
这是我的activity_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="@layout/fragment_navigation" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我的fragment_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
</ListView>
还有我的列表项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/navigation_item_text"
android:layout_gravity="center_horizontal" />
</LinearLayout>
【问题讨论】:
-
在某些设备上
START==LEFT在另一个==RIGHT上,它用于 RTL 从右到左的书写支持。在所有设备上使用START或LEFT以固定到选定的一侧 -
@MikeM。不,在我的 Java 代码中我不使用它;)
-
@Tobias :有什么特别的原因你在你的activity_drawer.xml 中使用
Fragment,然后只是使用一个只有ListView的布局吗?如果您从 activity_drawer.xml 中删除<fragment ... />块并将其替换为ListView会发生什么? -
您的抽屉需要在 LinearLayout 之外。
-
如果你仔细检查,你会发现确实有两个顶级元素:LinearLayout 和 fragment。