一、DrawerLayout侧滑栏在下
在使用DrawerLayout控件的时候,一般会设计到侧滑栏的位置,有些情况下需要把侧滑栏放在下方。代码如下:
<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">
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主界面布局-->
</RelativeLayout>
<LinearLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:orientation="vertical">
<!--侧滑栏布局-->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
在这个例子中,使用了LinearLayout作为侧滑栏,设置了布局的高度为250dp,设置了layout_gravity
为bottom
,即放置在底部。
二、DrawerLayout详解
DrawerLayout控件在Android设计中使用非常广泛且灵活,可以设计侧滑栏、抽屉等功能。以下是一些DrawerLayout的基本用法介绍:
1. DrawerLayout的基本结构
<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">
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主界面布局-->
</RelativeLayout>
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:orientation="vertical">
<!--侧滑栏布局-->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
其中,DrawerLayout控件包括三个元素:主界面布局、侧滑栏布局、DrawerLayout控件本身。
2. 打开与关闭DrawerLayout
使用DrawerLayout控件时,要实现打开与关闭侧滑栏,需要在Activity中编写以下代码:
// 打开侧滑栏
mDrawerLayout.openDrawer(GravityCompat.START);
// 关闭侧滑栏
mDrawerLayout.closeDrawer(GravityCompat.START);
其中,start
为常量,表示侧滑栏所在放置位置。
3. 监听DrawerLayout的打开与关闭事件
DrawerLayout控件支持监听侧滑栏打开与关闭的事件(如抽屉被打开、抽屉状态改变等)。监听DrawerLayout的打开与关闭事件,需要先设置Listener监听器,代码如下:
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerOpened(View view) {
// 抽屉被打开
}
@Override
public void onDrawerClosed(View view) {
// 抽屉被关闭
}
@Override
public void onDrawerSlide(View view, float v) {
// 抽屉状态改变
}
@Override
public void onDrawerStateChanged(int i) {
// 抽屉状态改变
}
});
4. 使用Fragment替换主界面布局
使用Fragment替换主界面布局,可以使得主界面布局更加灵活。使用Fragment替换主界面布局的方法如下:
Fragment fragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment).commit();
其中,R.id.content_frame
是主界面布局的ID。
三、layout
在Android开发中,layout是一个非常重要的概念。Layout用来设置布局,包括主界面布局、侧滑栏布局等。在使用DrawerLayout时,需要注意layout参数。代码如下:
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:orientation="vertical">
<!--侧滑栏布局-->
</LinearLayout>
在这个例子中,设置了LinearLayout的宽度为240dp,高度为match_parent
,也就是与DrawerLayout控件一样高。同时,在布局中使用了layout_gravity
参数,将侧滑栏放置在左侧(start),如下图所示: