如何替换已弃用的 android.support.v4.app.ActionBarDrawerToggle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26439619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:26:43  来源:igfitidea点击:

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

androidandroid-support-library

提问by AndreaF

Yesterday (17-10-2014) I have update Android SDK and support-library-v4.jarof my App, now I get deprecation warning related to ActionBarDrawerToggle, reading the documentationseems that I have to use the ActionBarDrawerTogglein support-library-v7.appcompact.jar.

昨天(2014 年 17 月 10 日)我更新了 Android SDK 和support-library-v4.jar我的应用程序,现在我收到了与 相关的弃用警告ActionBarDrawerToggle,阅读文档似乎我必须使用ActionBarDrawerTogglein support-library-v7.appcompact.jar.

Here some parts of my Activity that could be relevants:

这是我的活动中可能相关的一些部分:

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MyActivity extends FragmentActivity {
    private ActionBar bar;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawer;
    private ListView mDrawerList;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_infoviewer);

        bar = this.getActionBar();

        bar.setDisplayHomeAsUpEnabled(true);

        bar.setHomeButtonEnabled(true);
        bar.setDisplayShowTitleEnabled(false);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawer.setBackgroundColor(getResources().getColor(R.color.White));
        initNavMenu();
        try {
            mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }

        mDrawer.setDrawerListener(mDrawerToggle);
    }

    ....

    private void initNavMenu() {
        NavMenuAdapter mAdapter = MyDrawers.getDefaultDrawer(MyActivity.this, true);
        mDrawerList = (ListView) findViewById(R.id.drawer);
        mDrawerList.setBackgroundColor(getResources().getColor(R.color.GreenMoneyDark));
        if (mDrawerList != null) mDrawerList.setAdapter(mAdapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener(MyActivity.this, mDrawer, mDrawerList));
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

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

        @Override
        public void onDrawerClosed(View view) {
            bar.setTitle(getString(R.string.ns_menu_close));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            bar.setTitle(getString(R.string.ns_menu_open));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    }

}

I have tried to copy support-library-v7 and replace

我试图复制 support-library-v7 并替换

import android.support.v4.app.ActionBarDrawerToggle;

with

 import android.support.v7.app.ActionBarDrawerToggle;

this has caused compilation problem in

这导致了编译问题

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

So I have tried to replace R.drawable.action_drawerwith

所以我试图R.drawable.action_drawer

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

this compiles but crash at Runtime with

这编译但在运行时崩溃

 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$attr;
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:190)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:186)

Note that android-support-v7-appcompat.jaris correctly added in project dependencies enter image description here

注意android-support-v7-appcompat.jar在项目依赖中正确添加 在此处输入图片说明

采纳答案by Silverstorm

Adding only android-support-v7-appcompat.jarto 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

仅添加android-support-v7-appcompat.jar库依赖项是不够的,您还必须在项目中导入您可以在 SDK 路径中找到的模块,\android-sdk\extras\android\support\v7\appcompat然后添加模块依赖项以这种方式配置项目结构

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);
        }

回答by reVerse

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

您无需使用ActionBarDrawerToggle需要Toolbar 的超级调用。这意味着不要使用以下构造函数:

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:

所以基本上你唯一要做的就是删除你的 custom drawable

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

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

有关文档中“新”ActionBarDrawerToggle 的更多信息(单击)

回答by Abhi1227

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

你必须使用 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.

如果抽屉切换按钮变暗,那么您必须使用支持库中提供的 supportActionBar。

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

您可以从此链接实现 supportActionbar:http: //developer.android.com/training/basics/actionbar/setting-up.html

回答by erluxman

Insted of

插入的

drawer.setDrawerListener(toggle);

You can use

您可以使用

drawer.addDrawerListener(toggle);