安卓原来用的侧滑方式是使用第三方,也就是使用SliddingMenu。这个已经有点古老了,在此不多做介绍。
DrawerLayout 是一个抽屉容器,来自support-v4包里面的。(android.support.v4.widget)。
使用上主要是在xml布局文件中,
简单使用:主要是侧滑部分android:layout_gravity="start"这个属性,start是左侧,end是右侧
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hp.md.DrawerLayout_Activity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" ></android.support.v7.widget.Toolbar> <android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawerLayout" android:layout_below="@id/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hp.md.DrawerLayout_Activity"> <!--内容界面--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是内容界面" /> </LinearLayout> <!-- 侧滑菜单左侧部分 --> <LinearLayout android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="start" android:background="#0f0" android:orientation="vertical" android:paddingTop="50dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item3" /> </LinearLayout> <!-- 侧滑菜单右侧部分 --> <LinearLayout android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="end" android:background="#0f0" android:orientation="vertical" android:paddingTop="50dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item3" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
简单优化一了下,引入Toolbar,然后在Activity里面设置同步监听
toolbar = (Toolbar) findViewById(R.id.toolbar); //将actionBar替换成toolbar setSupportActionBar(toolbar); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close); //同步状态 actionBarDrawerToggle.syncState(); //给侧滑控件设置监听 drawerLayout.setDrawerListener(actionBarDrawerToggle);
--------------------------------------------------------------------------------------------------------简易版已经完成------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------DIY版本---------------------------------------------------------------------------------------------------------------
//给侧滑控件设置监听
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerStateChanged(int newState) { // 状态发生改变 // switch (newState) { // case DrawerLayout.STATE_DRAGGING: // Log.i(TAG, "拖动状态"); // break; // case DrawerLayout.STATE_IDLE: // Log.i(TAG, "静止状态"); // break; // case DrawerLayout.STATE_SETTLING: // Log.i(TAG, "设置状态"); // break; // default: // break; // } } @Override public void onDrawerSlide(View drawerView, float slideOffset) { // 滑动的过程当中不断地回调 slideOffset:0~1 View content = drawerLayout.getChildAt(0); View menu = drawerView; float scale = 1 - slideOffset;//1~0 float leftScale = (float) (1 - 0.3 * scale); float rightScale = (float) (0.7f + 0.3 * scale);//0.7~1 menu.setScaleX(leftScale);//1~0.7 menu.setScaleY(leftScale);//1~0.7 content.setScaleX(rightScale); content.setScaleY(rightScale); content.setTranslationX(menu.getMeasuredWidth() * (1 - scale));//0~width } @Override public void onDrawerOpened(View drawerView) { // 打开 } @Override public void onDrawerClosed(View drawerView) { // 关闭 } });