简介
可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。
drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),
主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)。
1.编写Activity的布局文件
根布局使用android.support.v4.widget.DrawerLayout
然后其内部第一个View为内容区域,第二个View为左侧菜单,第三个View为右侧侧滑菜单,当前第三个是可选的。
第一个View的宽高应当设置为match_parent,当然了,这也理所当然。
第二、三个View需要设置Android:layout_gravity=”left”,和android:layout_gravity=”right”且一搬高度设置为match_parent,
宽度为固定值,即侧滑菜单的宽度(宽度你写match_parent也行,但是不会全部覆盖)。
首先是mainactivity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mdrawerLayout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.soft.qianyu.myqqdddreawerlayout.MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#60f227"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:text="我是内容界面"/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开侧滑菜单"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#4b91f9"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_gravity="left"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:text="我是侧滑菜单"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
写完布局之后,你就可以实现侧滑了!但是此时的侧滑并不是将内容界面挤到右侧的侧滑,而是覆盖在内容界面上面的侧滑。此时若还要实现像QQ那样的侧滑,则还需要以下代码。
2,在代码中为相应的控件添加事件监听器
public class MainActivity extends Activity implements View.OnClickListener{
//1)声明DrawerLayout变量
DrawerLayout drawerLayout;
//声明内容页面变量
LinearLayout contentView ;
//声明按钮
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
init();
}
private void init() {
//2)实例化DrawerLayout控件
drawerLayout = (DrawerLayout) findViewById(R.id.mdrawerLayout);
//实例化按钮并设置点击监听事件
findViewById(R.id.bt1).setOnClickListener(MainActivity.this);
findViewById(R.id.bt2).setOnClickListener(MainActivity.this);
//3)为DrawerLayout控件添加监听器
drawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
//当侧滑菜单正在滑动时触发的方法
/**
第一个参数:正在滑动的侧滑菜单
第二个参数:菜单滑动的宽度的百分比
**/
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
//获得侧滑菜单的宽度
int drawerViewWidth = drawerView.getMeasuredWidth();
//根据滑动百分比计算内容部分应该向右边移动的距离
int marginLeft = (int)(drawerViewWidth * slideOffset) ;
//获得内容部分的View对象(内容View对象是第一个,所以是0)
contentView = (LinearLayout) drawerLayout.getChildAt(0);
//修改内容部分的左边距
contentView.setLeft(marginLeft);
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bt1:
drawerLayout.openDrawer(Gravity.LEFT);
break;
case R.id.bt2:
drawerLayout.closeDrawers();
break;
}
}
}