我在这里创建了自己的DrawerLayout是XML:
<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"
tools:context=".MainActivity">
<!-- The main content view -->
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/white"
android:dividerHeight="1dp"
android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>
默认情况下,我选择FrameLayout
代替LinearLayout
而不是LinearLayout layout = (LinearLayout) findViewById(R.id.content);
,因为它允许我设置边距/填充并自动相互添加元素。无论如何,当我打电话时:
FrameLayout
我得到一个例外,说不能从LinearLayout
投射到LinearLayout
,但我没有在任何地方定义{{1}}。
答案 0 :(得分:2)
你偶然发现了一个关于Android如何创建Layouts的有趣怪癖。
最顶层的布局是系统将布局放入的FrameLayout。其ID为android.R.id.content
。您可能会将android.R
文件导入到您的应用中,因此您引用了android.R.id.content
而不是项目的R
文件,该文件具有正确的ID。