与icon_drawable在navegation绘制对象问题对象、问题、icon_drawable、navegation

2023-09-12 03:24:03 作者:猫有九条命唯有一颗心

我想在动作条显示Icon_drawble,但是当R.drawable.ic_drawer是在动作条第一位置,显示返回箭头。

是这样的:

  mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerToggle =新ActionBarDrawerToggle(
                本,
                mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close

                ){
 

我需要证明这样的事情,但是code以上它不工作。

如果我改变R.drawable.ic_drawer到另一个位置时,它给我的跟随logcat的错误。

  mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerToggle =新ActionBarDrawerToggle(
                本,
                mDrawerLayout,

                R.string.drawer_open,
                R.drawable.ic_drawer,//其他位置
                R.string.drawer_close

                )
 
安卓项目实战之与UI那点事 图片适配你必须要了解的知识点

logcat的:

 了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.navegatiodrawer / com.example.navegatiodrawer.MainActivity}:android.content.res.Resources $ NotFoundException:文件从绘制资源ID打开抽屉式导航
 

解决方案

这似乎是最近AppCompat库版本的bug。

混乱的部分原因可能是由于该机器人工作室的新项目向导生成坏code,当你创建一个新的导航抽屉活动 - 它使用的 V4支持库 ActionBarDrawerToggle ,这是德precated。相反,它应该使用 V7支持库 ActionBarDrawerToggle

您有两种选择:

最好的选择就是切换到V7 ActionBarDrawerToggle 。要做到这一点,改变你的进口,以用 android.support.v7.app.ActionBarDrawerToggle 而不是 android.support.v4.app.ActionBarDrawerToggle 。如果您需要提出的是删除你的 ic_drawer 参数altogether-切换的较新版本的另一处改动产生你正在寻找自动汉堡包。

如果您坚持使用V4切换或自定义图标,就可以恢复到支持库的旧版本。使用生成的默认项目创建一个新的导航抽屉里活动时,我能够通过恢复到 com.android.support:appcompat-v7:22.1.0 在回避这个bug我build.gradle。

已经有中的问题跟踪更新向导生成的code的错误。我不希望V4的切换,以获得固定的,因为它是德precated。

I am trying to show Icon_drawble in a ActionBar, but when R.drawable.ic_drawer is in the first position show return arrow in actionbar.

like this:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open, 
                R.string.drawer_close

                ) { 

I need to show something like this, but the code above It doesnt work.

If I change R.drawable.ic_drawer to another position,it give me the follow logcat error.

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,

                R.string.drawer_open,
                R.drawable.ic_drawer,//another position
                R.string.drawer_close 

                ) 

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navegatiodrawer/com.example.navegatiodrawer.MainActivity}: android.content.res.Resources$NotFoundException: File Open navigation drawer from drawable resource ID

解决方案

This appears to be a bug in recent versions of the AppCompat library.

Part of the confusion is likely due to the fact that Android Studio's new project wizard generates bad code when you create a new navigation drawer activity- it uses the v4 support library ActionBarDrawerToggle, which is deprecated. Instead, it should use the v7 support library ActionBarDrawerToggle.

You have two options:

The best option is to switch to the v7 ActionBarDrawerToggle. To do this, change your imports to use android.support.v7.app.ActionBarDrawerToggle instead of android.support.v4.app.ActionBarDrawerToggle. The only other change you should need to make is removing your ic_drawer parameter altogether- the newer version of the toggle generates the hamburger you are looking for automatically.

If you insist on using the v4 toggle or a custom icon, you can revert to an older version of the support library. Using the default generated project when creating a new navigation drawer activity, I was able to sidestep this bug by reverting to com.android.support:appcompat-v7:22.1.0 in my build.gradle.

There is already a bug in the issue tracker for updating the wizard-generated code. I wouldn't expect the v4 toggle to get fixed since it is deprecated.