Android - Easy/comfortable 使用 Android Studio 设计 DrawerLayout 的方法?

Android - Easy/comfortable way to design DrawerLayout using Android Studio?

在我的应用程序中,我为其中一项活动实施了 DrawerLayout,并在 activity 的中心添加了一个 listView。为了添加项目(Buttons 和 textView 等),我通过编辑 XML 布局文件(通过向页面添加原始属性)手动添加它们。在Android Studio 中有什么方法可以像设计一个简单的Activity 一样设计抽屉吗?我添加了它在我的工作室中的外观的屏幕截图,所有元素都显示在页面左侧,模拟器之外。

编辑:这是我的 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">

    <!-- The first child in the layout is for the main Activity UI-->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity"
            android:background="#ffffffff">

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/listViewChat"
                android:layout_centerHorizontal="true"
                android:choiceMode="multipleChoice" />

        </RelativeLayout>

        <!-- Side navigation drawer UI -->
        <RelativeLayout
            android:id="@+id/drawerContainer"
            android:layout_width="270dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="@drawable/splash_background_blur"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:clickable="true">

            <!--this custom view serves as a dimmer for the drawer-->
            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#73000000"
                android:id="@+id/view1"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:focusableInTouchMode="true"/>


            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editTextUserName"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="160dp"
                android:cursorVisible="false"
                android:textColor="#FFF"
                android:textSize="20sp"
                android:background="@null"
                android:maxLength="15"
                android:inputType="textNoSuggestions"
                android:hint="Enter user name"/>

            <Button
                android:layout_width="220dp"
                android:layout_height="35dp"
                android:text="@string/buttonSelectPhoto"
                android:id="@+id/buttonTakePhoto"
                android:background="@drawable/splash_button"
                android:textColor="#FFF"
                android:layout_marginTop="200dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"/>

            <Button
                android:layout_width="220dp"
                android:layout_height="35dp"
                android:text="@string/buttonTakePhoto"
                android:id="@+id/buttonSelectPhoto"
                android:background="@drawable/splash_button"
                android:textColor="#FFF"
                android:layout_marginTop="250dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"
                android:layout_alignParentStart="true"/>

            <Button
                android:layout_width="220dp"
                android:layout_height="35dp"
                android:text="@string/buttonTakeATour"
                android:id="@+id/buttonTakeTour"
                android:background="@drawable/splash_button"
                android:textColor="#FFF"
                android:layout_marginTop="300dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"/>

            <!-- <ListView
                 android:id="@+id/navList"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>
            <LinearLayout android:focusable="true"
                android:focusableInTouchMode="true" android:layout_width="0px"
                android:layout_height="0px" />-->

        </RelativeLayout>

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


编辑
我将抽屉的内容拆分到一个单独的 xml 文件中,并尝试使用 <include> 标签添加此内容。当我单击 "hamburger"(打开抽屉)图标时,应用程序崩溃并出现 java.lang.IllegalArgumentException: No drawer view found with gravity LEFT 错误。我的布局有什么问题?


activity利用DrawerLayoutinclude新的独立布局(抽屉内容):

<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">

    <!-- The first child in the layout is for the main Activity UI-->
    <include layout="@layout/drawer_layout_and_content"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:background="#ffffffff">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewChat"
            android:layout_centerHorizontal="true"
            android:choiceMode="multipleChoice" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>  


这是自定义布局,用作主抽屉 activity:
(drawer_layout_and_content.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:background="@drawable/splash_background_blur"
    android:id="@+id/drawerContainer">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#73000000"
        android:id="@+id/view1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:focusableInTouchMode="true"/>
    <Button
        android:layout_width="220dp"
        android:layout_height="35dp"
        android:text="@string/buttonSelectPhoto"
        android:id="@+id/buttonTakePhoto"
        android:background="@drawable/splash_button"
        android:textColor="#FFF"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="25dp"
        android:layout_marginStart="25dp"/>
</RelativeLayout>

您可以使用 include 关键字来做到这一点。首先,创建一个名为 drawer_content.xml 的布局文件,然后执行 <include layout="@layout/drawer_content">。然后你编写 drawer_content.xml 就像 ActivityFragment 的布局文件一样。如果您想了解更多详情,请查看 here.