背景:之前项目中一直使用slidingmenu ,最近使用as开发,导包经常出现问题,算了,用官方的吧
首先官方的布局 sample里面 侧滑部分是listview。这里贴下 我的布局,
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dl"
>
<!-- 抽屉没有打开会显示的布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<!-- 抽屉打开后显示的布局-->
<FrameLayout
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_tab">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
标注很清楚了,这样子可以自定义了。
然后是代码部分:
<pre name="code" class="java"> DrawerLayout mDrawLayout;
ActionBarDrawerToggle drawerToggle;
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
setContentView(R.layout.activity_main);
mDrawLayout = (DrawerLayout) findViewById(R.id.dl);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
activity = this;
//参数1:当前actionbar所在的activity
//参数2:参数2控制是哪个抽屉
//参数3: 抽屉显示的图片 按钮的图片 参数4 抽屉打开的描述
drawerToggle = new ActionBarDrawerToggle(this,mDrawLayout,R.drawable.ic_drawer_am
,R.string.open_drawer,R.string.close_drawer){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(activity, "抽屉打开了", Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(activity, "抽屉关闭了", Toast.LENGTH_SHORT).show();
}
};
mDrawLayout.setDrawerListener(drawerToggle);
//让开关和actionbar建立联系
drawerToggle.syncState();
}
/** 处理actionBar菜单条目的点击事件 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}
记住 actionbar的
<pre name="code" class="java"> onOptionsItemSelected(
返回drawerToggle.onOptionsItemSelected(item)