处理 ActionBarDrawerToggle is Deprecated

阅读时间 约 1 分钟

更新Android Support Library后,自动构建DrawerFragmentLayout,会发现有这样一个Warning:

'Android.support.v4.app.ActionBarDrawerToggle' is deprecated

替换Support Library

查询官方文档可知,我们需要将v4包中的ActionBarDrawerToggle替换为support-library-v7.appcompact.jar中的ActionBarDrawerToggle

替换后,new ActionBarDrawerToggle的时候会发生编译错误:

image1

替换构造函数

根据StackOverFlow中的这个问题,新的Support Library已经不支持传入一个静态的drawer图片作为icon

我们将构造函数:

mDrawerToggle = new ActionBarDrawerToggle(
    getActivity(),
    mDrawerLayout,
    R.drawable.ic_drawer,
    R.string.navigation_drawer_open,
    R.string.navigation_drawer_close
    ) {...}

替换为:

mDrawerToggle = new ActionBarDrawerToggle(
    getActivity(),
    mDrawerLayout,
    R.string.navigation_drawer_open,
    R.string.navigation_drawer_close
    ) {...}

编译、运行,发现新的drawer icon已经变成炫酷的动画形式了!