旋转特效按钮ActionBarDrawerToggler的使用详情

来源:互联网 发布:大型科幻战争电影知乎 编辑:程序博客网 时间:2024/05/22 23:13

卷首语: v7兼容包提供一个给侧滑控件显示拖动状态的控件
与DrawerLayout侧拉菜单搭配使用

1 在onCreate方法中

 //创建一个 ActionBarDrawerToggle (参数可以通过Ctrl+Q提示出来)        Activity activity = this;//页面上下文        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);//侧滑菜单          int openDrawerContentDescRes = R.string.open;  //菜单显示时的中文描述        int closeDrawerContentDescRes = R.string.close; //菜单关闭时的中文描述        ActionBarDrawerToggle arrowBtn = new ActionBarDrawerToggle(this, drawerLayout, openDrawerContentDescRes, closeDrawerContentDescRes);        //设置显示条件为true        ActionBar actionBar = getSupportActionBar();        //设置显示返回箭头        actionBar.setDisplayHomeAsUpEnabled(true);        //设置显示三横杠        arrowBtn.syncState();        //添加菜单拖动监听事件  根据菜单的拖动距离 将距离折算成旋转角度        drawerLayout.addDrawerListener(arrowBtn);        //处理旋转按钮的点击事件。注意这个控件ID为 android.R.id.home

2 添加点击事件

                case android.R.id.home:                View menu = findViewById(R.id.fl_menu);                if (drawerLayout.isDrawerOpen(menu)) {//判断内容是否可见                    drawerLayout.closeDrawer(menu);//关闭                } else {                    drawerLayout.openDrawer(menu);//开启                }                break;

3 xml界面布局

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!--页面内容-->    <FrameLayout        android:id="@+id/fl_main"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#ACFFAC">    </FrameLayout>    <!--页面的菜单-->    <FrameLayout        android:id="@+id/fl_menu"        android:layout_width="280dp"        android:layout_height="match_parent"        android:layout_gravity="start"        android:background="#1FF0FF">    </FrameLayout></android.support.v4.widget.DrawerLayout>
0 0