[英]Android - NavigationView, Menu with icon as TexDrawable
I am trying set the icon of item menu inside NavigationView as TextDrawable. 我正在尝试将NavigationView中项目菜单的图标设置为TextDrawable。 For textDrawable im using TextDrawable library , but once i set the icon for the menu item it only displays a black circle instead of generated drawable.
对于使用TextDrawable库的 textDrawable im,但是一旦我为菜单项设置了图标,它只会显示一个黑色圆圈,而不是生成的drawable。
I did try my code to generate the TextDrawable outside the NavigationView and it displays want i need, but I can't manage to make it display as icon for my menu item. 我确实尝试过在我的代码中在NavigationView之外生成TextDrawable,并且它显示了我所需的显示,但是我无法设法使其显示为菜单项的图标。
To generate the TexDrawable: 生成TexDrawable:
private Drawable getLetterDrawable(String letter){
return TextDrawable.builder()
.beginConfig()
.width(20) // width in px
.height(20) // height in px
.endConfig()
.buildRect(letter, Color.RED);
}
To set the icon: 设置图标:
...
navigationView.inflateMenu(R.menu.menu_drawer_users);
Menu menu = navigationView.getMenu();
menu.add("Item text here").setIcon(getLetterDrawable("A"));
...
And this is the unexpected result: 这是意外的结果:
EDIT: When I set R.mipmap.ic_launcher
as icon i get the android icon but also in grey color, so i can only see its border. 编辑:当我将
R.mipmap.ic_launcher
设置为图标时,我得到的是android图标,但也是灰色的,所以我只能看到其边框。 It seems that the drawable its being "tinted" when using menuItem.setIcon()
. 使用
menuItem.setIcon()
时,可绘制对象似乎已“着色”。
I did try to do the following, but without success: 我确实尝试执行以下操作,但未成功:
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_drawer, menu);
menu.add("Test");
menu.getItem(0).setIcon(R.mipmap.ic_launcher);
menu.getItem(0).getIcon().clearColorFilter();
menu.getItem(0).getIcon().invalidateSelf();
Question: How can I remove the filter and show my icon in colors? 问题:如何删除过滤器并用颜色显示图标?
EDIT 2 : After Andrea Basso
suggestion, I did manage to remove the tint from navigationView object, but i can't still see the letter inside the square. 编辑2:在
Andrea Basso
建议之后,我确实设法删除了NavigationView对象的色彩,但我仍然看不到正方形内的字母。 I only see the red square but no letter inside. 我只看到红色方块,但里面没有字母。
The main problem remains: how to put TextDrawable as icon on MenuItem
? 主要问题仍然存在:如何将TextDrawable作为图标放在
MenuItem
?
NavigationView
has the method setItemIconTintList (ColorStateList tint)
, which is what you're looking for. NavigationView
具有要查找的方法setItemIconTintList (ColorStateList tint)
。
A ColorStateList
is an XML file that defines the different filters for different states. ColorStateList
是一个XML文件,为不同的状态定义了不同的过滤器。 Here 's the documentation page. 这是文档页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.