我的主要活动
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
xml是:
<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:background="@android:color/black" >
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/sample_content_fragment"
android:background="#242a31"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0px" >
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view.
-->
<RelativeLayout
android:layout_width="240dp"
android:layout_gravity="start"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/left_LinearLayout"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
</LinearLayout>
<ListView
android:below="@+id/left_LinearLayout"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:listSelector="@drawable/selector_bg_key"
android:background="@color/default_color"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
但是应用程序因错误而崩溃:
android.widget.LinearLayout$LayoutParams cannot be cast to
android.support.v4.widget.DrawerLayout$LayoutParams
我需要在左侧菜单中添加编辑字段,或者我应该通过
的适配器添加它答案 0 :(得分:3)
你可能正在尝试类似的事情:
mDrawerLayout.openDrawer(mDrawerList);
但是,ListView
的父级不是DrawerLayout
。您现在应该使用RelativeLayout
作为抽屉:
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/left_LinearLayout"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
</LinearLayout>
<ListView
android:layout_below="@+id/left_LinearLayout"
android:id="@+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</RelativeLayout>
获取布局和列表:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawer = (RelativeLayout) findViewById(R.id.left_drawer);
然后像这样打开抽屉:
mDrawerLayout.openDrawer(mDrawer);