EditText inside of a DrawerLayout

2019-04-10 12:27发布

问题:

I've been trying to set an EditText box inside of a DrawerLayout, but reading carefully through the Android Training Website, they explain that the DrawerLayout is allowed to have only two child views. If I would like to do something like the next code, How should I approach it?

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<EditText
    android:id="@+id/EditText01"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:hint="Search" >
</EditText>
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

回答1:

Do it like this:

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
    android:id="@+id/left_drawer"
    android:layout_height="wrap_content"
    android:layout_width="240dp"
    android:orientation="vertical"
    android:layout_gravity="start" >
    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search" >
    </EditText>
    <ListView android:id="@+id/left_drawer_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

In other words, whatever the first view is, will be set as the content view, and whatever the second view is will be set in the drawer. So simply add elements inside the second view for drawer, and first for content. Cheers :)