Android-使用Android Studio轻松/舒适地设计抽屉布局?
在我的应用程序中,我为其中一个活动设置了Android-使用Android Studio轻松/舒适地设计抽屉布局?,android,android-studio,drawerlayout,Android,Android Studio,Drawerlayout,在我的应用程序中,我为其中一个活动设置了DrawerLayout,并在活动中心添加了一个listView。为了添加项目(按钮s和文本视图等),我通过编辑XML布局文件(通过向页面添加原始属性)手动添加它们。有没有办法像设计简单的活动那样在Android Studio中设计抽屉?我添加了一个屏幕截图,显示了它在我的工作室中的外观,所有元素都显示在页面左侧,模拟器外部 编辑:以下是我的XML布局: <android.support.v4.widget.DrawerLayout xml
DrawerLayout
,并在活动中心添加了一个listView
。为了添加项目(按钮
s和文本视图
等),我通过编辑XML布局文件(通过向页面添加原始属性)手动添加它们。有没有办法像设计简单的活动那样在Android Studio中设计抽屉?我添加了一个屏幕截图,显示了它在我的工作室中的外观,所有元素都显示在页面左侧,模拟器外部
编辑:以下是我的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>
以下是在主活动中用作抽屉的自定义布局:
(drawer\u layout\u和\u content.xml)
您可以使用include
关键字来完成此操作。首先,创建一个名为drawer\u content.xml
的布局文件,然后执行
。然后编写drawer\u content.xml
,就像编写活动
或片段的布局文件一样。如果您想了解更多详细信息,请查看。发布您的布局xml。这很有趣。你的意思是,我需要创建一个RelativeLayout
作为一个独立的xml文件,将所有需要的内容放在其中,然后,只需将它包含在我选择的抽屉布局中即可?@undroid是的,非常感谢。我试图将其分离为一个独立的抽屉
布局,该布局包含在我的主要活动中,我得到一个异常java.lang.IllegalArgumentException:未找到重力左侧的抽屉视图
。你能看看我刚刚添加到原始帖子中的内容吗?这是否应该以某种方式在清单中声明?你应该在你的抽屉布局和内容的根视图中设置android:layout\u gravity=“left | start”
。
<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>
<?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>