ActionBarDrawerToggle

ActionBarDrawerToggle 是 DrawerLayout.DrawerListener实现。和 NavigationDrawer 搭配使用,推荐用这个方法,符合Android design规范。 
作用:

  1. 改变android.R.id.home返回图标。
  2. Drawer拉出、隐藏,带有android.R.id.home动画效果。
  3. 监听Drawer拉出、隐藏

DrawerLayout.DrawerListener这个接口提供了回调抽屉事件, 如onDrawerOpened()和onDrawerClosed (),ActionBarDrawerToggle实现了DrawerLayout.DrawerListener. 所以你仍然可以覆盖这些回调, 但它也有助于正确的交互行为, 在工具栏的图标和导航抽屉之间。它的构造方法, 这就需要以下参数:

  1. 持有抽屉的Activity.
  2. DrawerLayout对象.
  3. 一个Drawable资源作为抽屉指示器.
  4. 字符串资源描述”打开抽屉”动作.
  5. 字符串资源描述”关闭抽屉”动作.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

ps:貌似这个ActionBarDrawerToggle原来是在android.support.v4.app包下,现在换到了android.support.v7.app这个包。也就是v7包。值得注意的是,然后它的构造方法是有变化的,上边的例子使用的就是v7包中的ActionBarDrawerToggle,我们来看一下他的构造方法是怎么定义的:

public ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, @StringRes int openDrawerContentDescRes, @StringRes int closeDrawerContentDescRes) {
……
……      
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

与原来的相比,这个里面多加了一个 ToolBar 的参数,因为我没有用过v4包中的ActionBarDrawerToggle,具体这里有什么变化还需要日后的研究。

posted @ 2016-12-15 20:14  天涯海角路  阅读(609)  评论(0)    收藏  举报