DrawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如Slidingmenu等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单内容和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现).
1.drawerLayout其实就是一个布局控件,跟LinearLayout等控件是一种东西,
但drawerLayout带有滑动功能,只要按照drawerLayout的规定布局方式完成布局,
就能有侧滑效果.
布局xml文件需要注意两点:
a,主内容区布局要放在侧滑菜单布局前面,这可以帮助DrawerLayout判断谁是侧滑菜单,谁是主内容区;
b,侧滑菜单部分的布局(这里是ListView)可以设置layout_gravity属性,她表示侧滑菜单是在左边还是右边.
.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" >
"match_parent"
android:layout_height="match_parent" >
"@+id/lv_drawer"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="right"
android:background="#111" />
.support.v4.widget.DrawerLayout>
2.drawerLayout侧滑菜单页面的展开与隐藏可以被DrawerLayout.DrawerListener的实现监听到,这样你就可以在菜单展开与隐藏发生的时刻做一些希望做的事情.
private class DrawerItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
lv_drawer.setItemChecked(position, true);
btn_test.setText(lv_content[position]);
mDrawerLayout.closeDrawers();
}
}
3.侧边菜单其实只是一个普通的View,一般里面装的是ListView,看起来像菜单,他完全可以是一个button,textview等等.
4.在代码中主动展开与隐藏侧滑菜单,在点击侧滑菜单选项的时候我们往往需要隐藏多个菜单来显示整个菜单对应的内容,DrawweLayout.closeDrawer方法用于隐藏侧边菜单,DrawerLayout.openDrawer方法用于展开侧边菜单.
范例代码如下:
a.Main Activity.java
public class MainActivity extends Activity implements OnClickListener {
private Button btn_test;
private ListView lv_drawer;
private DrawerLayout mDrawerLayout;
private String[] lv_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mDrawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
lv_content = getResources().getStringArray(R.array.test_array);
lv_drawer=(ListView) findViewById(R.id.lv_drawer);
lv_drawer.setAdapter(new ArrayAdapter(this,
R.layout.lv_list_item,lv_content));
lv_drawer.setOnItemClickListener(new DrawerItemClickListener());
btn_test=(Button) findViewById(R.id.btn_test);
btn_test.setOnClickListener(this);
}
private class DrawerItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
lv_drawer.setItemChecked(position, true);
btn_test.setText(lv_content[position]);
mDrawerLayout.closeDrawers();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_test:
mDrawerLayout.openDrawer(Gravity.END);
break;
default:
break;
}
}
}
b. activity_main.xml
.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" >
"match_parent"
android:layout_height="match_parent" >
"@+id/lv_drawer"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="right"
android:background="#111" />
.support.v4.widget.DrawerLayout>
c.arrays.xml
xml version="1.0" encoding="utf-8"?>
<string-array name="test_array">
<item>test oneitem>
<item>test twoitem>
<item>test threeitem>
<item>test fiveitem>
<item>test sixitem>
<item>test sevenitem>
<item>test eightitem>
<item>test nineitem>
<item>test tenitem>
<item>test elevenitem>
<item>test twelveitem>
<item>test thirteenitem>
<item>test fourteenitem>
<item>test fifteenitem>
<item>test sixteenitem>
<item>test seventeenitem>
string-array>