简体   繁体   English

Android - 如何禁用菜单项但删除默认的灰色文本颜色?

[英]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 :您可以使用ForegroundColorSpanspanString添加更多跨度以设置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.

相关问题 Android菜单项文字颜色 - Android menu item text colour Android Honeycomb-如何使选项菜单项看起来被禁用? - android honeycomb - how to make options menu item look disabled? 如何将所有文本颜色更改为默认颜色(文本为黑色,提示为灰色)而不是白色 - How to change all text colour to default colour (Black for Text, Grey for Hint) instead of white 更改默认文本颜色并仍显示不同颜色的禁用菜单项 - Changing default text colour and still showing disabled menu items in different colour Android:onCreateOptionsMenu-删除默认菜单项 - Android: onCreateOptionsMenu - Remove Default Menu Item 微调框有灰色文字,为什么? 如何将其更改为默认文本颜色? - Spinner has Grey Text, why? How can I change it to default text colour instead? 如何为NavigationView中的单个菜单项提供自定义文本颜色? - How to give custom text colour for a single menu item in a NavigationView? 如何在Android工具栏上单击禁用的选项菜单项? - How to click disabled option menu item in Toolbar android? 我们如何删除默认的Android设备颜色 - How can we remove default Android Device colour 如何在Android中为新菜单项设置onclicklistener - How to make onclicklistener for new menu item in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM