参考文献:file:///D:/android/sdk/docs/training/implementing-navigation/nav-drawer.html
使用DrawerLayout APIs 通过Support Library :android.support.v4.app
在layout中添加布局样式:
<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"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> </android.support.v4.widget.DrawerLayout>第一个布局为可显示的布局与<LinearLayout>类似,第二存放你要侧滑的内容
通过ActionBarDrawerToggle方法使用action bar来监听drawlout。
mDrawerToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close ){ public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } };; mDrawerLayout.setDrawerListener(mDrawerToggle);在menu中单击事件出发右侧栏mDrawerToggle.onOptionsItemSelected(item)
@Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle your other action bar items... return super.onOptionsItemSelected(item); } 如果不使用action bar 侧滑通过手指右滑实现显示 getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setHomeButtonEnabled(false);