Java 导航抽屉项目图标未显示原始颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31394265/
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-11 11:02:05  来源:igfitidea点击:

Navigation drawer item icon not showing original colour

javaandroidxmlandroid-support-library

提问by MacaronLover

I'm trying to show an icon next to an item within my menu for my navigation drawer, but for some reason the icon always appears in grey rather than the original colour (brown). Is there any way of preventing this from happening in order to show the icon's original colour?

我正在尝试在我的导航抽屉菜单中的项目旁边显示一个图标,但由于某种原因,该图标始终显示为灰色而不是原始颜色(棕色)。有没有办法防止这种情况发生以显示图标的原始颜色?

MainActivity.java

主活动.java

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }
    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();

                return true;
            }
        });
    }
}

drawer_view.xml

drawer_view.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Section">
        <menu>
            <item
                android:id="@+id/navigation_item_1"
                android:icon="@drawable/ic_browncircle"
                android:title="Sub item 1" />
        </menu>
    </item>
</menu>

enter image description here

在此处输入图片说明

采纳答案by Chris

I found the answer here: https://stackoverflow.com/a/30632980/875249

我在这里找到了答案:https: //stackoverflow.com/a/30632980/875249

To avoid the link its pretty straightforward:

为了避免链接它非常简单:

    mNavigationView.setItemIconTintList(null);

This disables all state based tinting, but you can also specify your own list too. It worked great for me!

这将禁用所有基于状态的着色,但您也可以指定自己的列表。它对我很有用!

Here is where you can get the details on creating a color state list, but its pretty simple too: http://developer.android.com/reference/android/content/res/ColorStateList.html

您可以在此处获取有关创建颜色状态列表的详细信息,但它也非常简单:http: //developer.android.com/reference/android/content/res/ColorStateList.html

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="@color/primary" />
        <item android:state_checked="false" android:color="@android:color/white" />
    </selector>

回答by Austin Hodak

You can try using a tinted drawable, not sure if it works below 5.0.

您可以尝试使用有色可绘制对象,不确定它是否可以在 5.0 以下运行。

Create a drawable and add the following code.

创建一个 drawable 并添加以下代码。

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_browncircle"
    android:tint="@color/brownColor"/>

And then change your menu item drawable to the one you just created. If that doesn't work, then I'm not sure of any other solutions. You can try this library: https://github.com/mikepenz/MaterialDrawerI use it a lot in my projects.

然后将您的菜单项 drawable 更改为您刚刚创建的菜单项。如果这不起作用,那么我不确定任何其他解决方案。你可以试试这个库:https: //github.com/mikepenz/MaterialDrawer我在我的项目中经常使用它。

回答by Ye Lin Aung

I've tried something similar in one of my app. And yes, it appears that the icon color doesn't change. But I've managed to do with another workaround. Here's my ic_browncircle.xml

我在我的一个应用程序中尝试过类似的东西。是的,图标颜色似乎没有改变。但我设法用另一种解决方法来做。这是我的ic_browncircle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:tint="@color/brown"
    >
  <size
      android:height="3dp"
      android:width="3dp"
      />
  <solid android:color="@color/brown"/>
</shape>

Which I believe is something similar to you but it doesn't have any effect and doesn't change the color.

我相信这与您相似,但它没有任何效果并且不会改变颜色。

So what I did is this.

所以我所做的是这个。

navigationView.getMenu()
    .findItem(R.id. navigation_item_1)
    .getIcon()
    .setColorFilter(Color.parseColor("#b69260"), PorterDuff.Mode.SRC_ATOP);

And it seems working. Here's the result.

它似乎工作。结果如下。

enter image description here

在此处输入图片说明

回答by Chuck

Use

    mNavigationView.setItemIconTintList(null);

it's right. Also If all your icons in one color scheme (i had all white) you can setup through xml file - app:itemIconTint="@android:color/white"

这是正确的。此外,如果您的所有图标都采用一种配色方案(我全是白色),您可以通过 xml 文件进行设置 - app:itemIconTint="@android:color/white"

My case:

我的情况:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:clickable="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/activity_main_drawer"
    android:background="@android:color/black"
    app:itemIconTint="@android:color/white"
    />

回答by Harsh Singhal

Just add one line in xml

只需在xml中添加一行

app:itemIconTint="@color/white"

app:itemIconTint="@color/white"

回答by Null Pointer Exception

Add this

添加这个

 android:tint="@color/colorPrimary"

回答by David Adam

If you create a project with navigation drawer which the Android Studioprovided. In your Main Activity class, you can just simply add this line of code navigationView.setItemIconTintList(null);to your onCreatemethod. Like this;

如果您使用提供的导航抽屉创建项目Android Studio。在您的 Main Activity 类中,您只需将这行代码添加navigationView.setItemIconTintList(null);到您的onCreate方法中即可。像这样;

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
 navigationView.setNavigationItemSelectedListener(this);
 navigationView.setItemIconTintList(null); // <----- HERE
 setupDrawerContent(navigationView);

回答by Parban

Some how this code not workingMainActivity.java

这段代码如何不工作MainActivity.java

                NavigationView.setItemIconTintList(null); // not working

so you can use it.

所以你可以使用它。

MainActivity.java

主活动.java

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
   navigationView.setNavigationItemSelectedListener(this); 
   navigationView.setItemIconTintList(null); // <-- HERE add this code for icon color