How to replace deprecated android.support.v4.app.ActionBarDrawerToggle

后端 未结 4 1945
难免孤独
难免孤独 2020-11-28 21:49

Yesterday (17-10-2014) I have update Android SDK and support-library-v4.jar of my App, now I get deprecation warning related to ActionBarDrawerToggle

相关标签:
4条回答
  • 2020-11-28 21:55

    Insted of

    drawer.setDrawerListener(toggle);
    

    You can use

    drawer.addDrawerListener(toggle);
    
    0 讨论(0)
  • 2020-11-28 22:04

    you must use import android.support.v7.app.ActionBarDrawerToggle;

    and use the constructor

    public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout)
    {
        super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close);
    }
    

    and if the drawer toggle button becomes dark then you must use the supportActionBar provided in the support library.

    You can implement supportActionbar from this link: http://developer.android.com/training/basics/actionbar/setting-up.html

    0 讨论(0)
  • 2020-11-28 22:15

    Adding only android-support-v7-appcompat.jar to library dependencies is not enough, you have also to import in your project the module that you can find in your SDK at the path \android-sdk\extras\android\support\v7\appcompatand after that add module dependencies configuring the project structure in this way

    enter image description here

    otherwise are included only the class files of support library and the app is not able to load the other resources causing the error.

    In addition as reVerse suggested replace this

    public CustomActionBarDrawerToggle(Activity mActivity,
                                               DrawerLayout mDrawerLayout) {
                super(mActivity, mDrawerLayout,new Toolbar(MyActivity.this) ,
                        R.string.ns_menu_open, R.string.ns_menu_close);
            }
    

    with

    public CustomActionBarDrawerToggle(Activity mActivity,
                                               DrawerLayout mDrawerLayout) {
                super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close);
            }
    
    0 讨论(0)
  • 2020-11-28 22:19

    There's no need for you to use super-call of the ActionBarDrawerToggle which requires the Toolbar. This means instead of using the following constructor:

    ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)
    

    You should use this one:

    ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)
    

    So basically the only thing you have to do is to remove your custom drawable:

    super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close);
    

    More about the "new" ActionBarDrawerToggle in the Docs (click).

    0 讨论(0)
提交回复
热议问题