导航抽屉项目图标不显示原始颜色 [英] 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
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
<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>
推荐答案
我在这里找到答案: 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!
在这里你可以获得有关创建颜色状态列表的详细信息,但它也非常简单:
http://developer.android.com/reference/android/content/res/ColorStateList.html
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
<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>
这篇关于导航抽屉项目图标不显示原始颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!