设置导航查看菜单项编程风格

问题描述:

是否可以设置菜单项的风格编程新的导航看法?我动态构建菜单,并且不能使用静态XML文件。我一直无法找到这方面的消息。

Is it possible to set the style of a MenuItem in the new navigation view programmatically? I am building the menu dynamically and cannot use a static XML file. I've been unable to find any information on this.

更新:我所熟悉的设置标题和图标,但不知道如何我可以如设置文本的Alpha。

UPDATE: I am familiar with setting title and icon, but not how I can e.g. set the alpha of the text.

更新2:在类NavigationView有 setItemTextColor(ColorStateList)。至于我可以告诉它是不是可以设置单个项目的颜色?
谢谢

UPDATE 2: In the class NavigationView there is setItemTextColor(ColorStateList). As far as I can tell it is not possible to set individual item colors? Thanks

如果您要设置菜单图标:

If you want to set menu icon:

第一套 navigationView.setItemIconTintList(空)

NavigationView navigationView = (NavigationView) findViewById(R.id.main_nav_view);
navigationView.setItemIconTintList(null);

和获得菜单项,设置图标吧。

and get the menuitem, set icon for it.

MenuItem menuItem = navigationView.getMenu().getItem(0);
menuItem.setIcon(R.drawable.nav_new_drawable);

和还可以设置文字的颜色,像这样的:

And also can set text color, like this:

navigationView.setItemTextColor(ColorStateList textColor);

和设置项目的背景下,类似这样的:

And set item background, like this:

navigationView.setItemBackgroundResource(int resId)

---编辑---

---Edit---

既然你已经知道上述情况,你可以试试下面code。

Since you already know the above, you can try below code.

<color name="test_alpha_color">#40999999</color>

MenuItem menuItem = navigationView.getMenu().getItem(0);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.test_alpha_color)), 0, s.length(), 0);
menuItem.setTitle(s);