简体   繁体   English

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

[英]Navigation drawer item icon not showing original colour

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抽屉视图.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>

在此处输入图片说明

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>

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"
    />

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.这是结果。

在此处输入图片说明

If you create a project with navigation drawer which the Android Studio provided.如果您创建一个带有Android Studio提供的导航抽屉的项目。 In your Main Activity class, you can just simply add this line of code navigationView.setItemIconTintList(null);在您的 Main Activity 类中,您只需添加这行代码navigationView.setItemIconTintList(null); to your onCreate method.到您的onCreate方法。 Like this;像这样;

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

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.然后将您的菜单项 drawable 更改为您刚刚创建的菜单项。 If that doesn't work, then I'm not sure of any other solutions.如果这不起作用,那么我不确定任何其他解决方案。 You can try this library: https://github.com/mikepenz/MaterialDrawer I use it a lot in my projects.你可以试试这个库: https : //github.com/mikepenz/MaterialDrawer我在我的项目中经常使用它。

只需在xml中添加一行

app:itemIconTint="@color/white"

Some how this code not working MainActivity.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

I found a solution.我找到了解决办法。

1) Go to the Design tab 1)转到设计选项卡
2) Click on navView 2)点击导航视图
3) Search itemicontint in properties 3)在属性中搜索itemicontint
4) Write null and press Enter 4)null然后回车

添加这个

 android:tint="@color/colorPrimary"
 BottomNavigationView navigationbtn = findViewById(R.id.bottomNavigationView);

 BottomNavigationMenuView mbottomNavigationMenuView =
                (BottomNavigationMenuView) navigationbtn.getChildAt(0);

       View view3 = mbottomNavigationMenuView.getChildAt(2);

       BottomNavigationItemView itemView3 = (BottomNavigationItemView) view3;

        itemView3.setIconTintList(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM