Android DrawerLayout实现侧滑菜单界面布局
DrawerLayout是Support Library包中的控件,实现了侧滑菜单效果。
按照drawerLayout的规定布局方式写完布局,就能有侧滑的效果。如:
<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">
<FrameLayout android:id="@ id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="@ id/left_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/black"/>
</android.support.v4.widget.DrawerLayout>
DrawerLayout最好为界面的根布局,否则可能出现触摸事件被屏蔽。
侧滑菜单被点击的时候,主内容区如果内容复杂,用Fragment去填充更容易,当然也可以使用其他视图控件。
DrawerLayout.closeDrawer方法用于隐藏侧边菜单,DrawerLayout.openDrawer方法用于展开侧边菜单。
drawerLayout左侧菜单(或者右侧)的展开与隐藏可以被DrawerLayout.DrawerListener的实现监听到
mDrawerToggle = new ActionBarDrawerToggle(
this, // host Activity
mDrawerLayout, // DrawerLayout object
R.drawable.ic_drawer, // nav drawer image to replace 'Up' caret
R.string.drawer_open, // "open drawer" description for accessibility
R.string.drawer_close // "close drawer" description for accessibility
) {
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);