Changing default text colour and still showing disabled menu items in different colour

a 夏天 提交于 2020-01-01 12:35:19

问题


With Theme.Holo.Light as the base theme, a designer noticed that the default text colour is not black, but a dark grey (#505050). We'd like to change it to black.

Looking for a simple way to change the default to black everywhere in the app, I found that this works:

<resources>    
    <style name="MyAppTheme" parent="android:Theme.Holo.Light">
        <item name="android:textColor">@android:color/black</item>
    </style>
</resources>

Now, problem is, that also changes the colour of disabled items in Action Bar's overflow menu. How to override default text colour while still having disabled menu items look "disabled"?

The menu should look something like below, but using android:textColor as above, it changes all the items to black.

I was experimenting with textColorPrimaryInverse, textColorPrimaryDisableOnly, textColorPrimaryInverseDisableOnly and disabledAlpha but those didn't seem to affect the overflow menu.


回答1:


You can use a drawable as the text colour, and in drawable you can use selector to select the colour according to enabled status. Using following drawable definition as colour will make your disabled menu items grey and the rest black.

In e.g. res/drawable/default_text_colour.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@android:color/darker_gray"/>
    <item android:color="@android:color/black"/>
</selector>

Then, using the drawable:

<item name="android:textColor">@drawable/default_text_colour</item>


来源:https://stackoverflow.com/questions/21279319/changing-default-text-colour-and-still-showing-disabled-menu-items-in-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!