写好Drawerlayout布局
实现抽屉布局
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/dl">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_tab"
android:layout_gravity="left">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
拿到ActionBar和新建ActionBarDrawerToggle对象
控制抽屉的开关, 显示在actionBar 上面
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(
true);
actionBar.setHomeButtonEnabled(
true);
drawerToggle =
new ActionBarDrawerToggle(
this,
mDrawerLayout, R.drawable.ic_drawer_am, R.string.open_drawer,
R.string.close_drawer){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(getApplicationContext(),
"抽屉关闭了",
0).show();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(getApplicationContext(),
"抽屉打开了",
0).show();
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
/** 处理actionBar菜单条目的点击事件 */
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(),
"搜索",
0).show();
}
return drawerToggle.onOptionsItemSelected(item)|
super.onOptionsItemSelected(item);
}
转载请注明原文地址: https://ju.6miu.com/read-971035.html