[英]Android - How to make a menu item disabled but remove the default grey text colour?
My menu file menu_main.xml , the "Create" item is just a title, so it should be disabled.我的菜单文件menu_main.xml ,“创建”项只是一个标题,所以应该禁用它。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.amazon.device.controllermanager.docsuilocal.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:icon="@drawable/ic_add"
android:title="@string/menu_create"
app:showAsAction="always">
<menu>
<group android:id="@+id/sorting" >
<item
android:id="@+id/title"
android:title="Create"
app:showAsAction="never" />
</group>
<item
android:id="@+id/action1"
android:title="Document"
app:showAsAction="never" />
<item
android:id="@+id/action2"
android:title="Spreadsheet"
app:showAsAction="never" />
<item
android:id="@+id/action3"
android:title="Presentation"
app:showAsAction="never" />
</menu>
</item>
</menu>
The id @id/title is just a title, hence it shouldn't be clickable, but it should be bold in color, so I do this in my java code id @id/title 只是一个标题,因此它不应该是可点击的,但它的颜色应该是粗体,所以我在我的 java 代码中这样做
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem createItem = menu.findItem(R.id.title);
SpannableString spanString = new SpannableString(createItem.getTitle().toString());
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
createItem.setTitle(spanString);
createItem.setCheckable(false);
createItem.setEnabled(false);
MenuCompat.setGroupDividerEnabled(menu, true);
return true;
}
The problem is it gets the grey disabled colour, but I want it have bold text and to not be clickable, as its purpose is to act like a title.问题是它获得了灰色禁用颜色,但我希望它具有粗体文本并且不可点击,因为它的目的是像标题一样。
Anyway I could overwrite the grey colour in this particular menu item alone?无论如何,我可以单独覆盖这个特定菜单项中的灰色吗?
You can add one more span to the spanString
to se the foreground color using ForegroundColorSpan
:您可以使用
ForegroundColorSpan
向spanString
添加更多跨度以设置ForegroundColorSpan
:
spanString.setSpan(new ForegroundColorSpan(ResourcesCompat
.getColor(resources, R.color.gray, null)),
0, spanString.length, 0);
The color value is <color name="gray">#606060</color>
颜色值为
<color name="gray">#606060</color>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.