Android: change color of disabled text using Theme/Style?

与世无争的帅哥 提交于 2019-12-18 18:54:50

问题


I have the following colors defined in my color.xml:

<color name="gold">#d49e43</color>
<color name="gold_disabled">#80d49e43</color>

And the following theme:

<style name="Theme.Example" parent="@style/Theme.Sherlock">       
    <item name="android:textColor">@color/gold</item>
</style>

In my SettingsActivity, I have a CheckBoxPreference and a Preference that depends on it. When The CheckBoxPreference is unchecked, the Preference is disabled, however, because of the custom gold text color that I set, it doesn't get "greyed out" like it does with the default color. How do I change this in XML? I've tried setting:

    <item name="android:textColorPrimaryDisableOnly">@color/gold_disabled</item>
    <item name="android:textColorPrimaryInverseDisableOnly">@color/gold_disabled</item>
    <item name="android:textColorPrimaryNoDisable">@color/gold_disabled</item>
    <item name="android:textColorSecondaryNoDisable">@color/gold_disabled</item>
    <item name="android:textColorPrimaryInverseNoDisable">@color/gold_disabled</item>
    <item name="android:textColorSecondaryInverseNoDisable">@color/gold_disabled</item>

but nothing seems to work.


回答1:


I know I'm late. However, I had exactly same problem and I fixed just now.

I found a way to fix it using resource files only. I found the answer here: https://stackoverflow.com/a/17123161/4860513

Basically, you can create a color selector under: res/color/

Note: You must create folder color if it does not exist.

For me, I did it:

res\color\primary_text_color_selector.xml (For Title)

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

res\color\secondary_text_color_selector.xml (For Summary)

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

Then, In my preference style, I did:

res\values\styles.xml

<!-- Preference Screen Theme --> 
<style name="AppTheme_PreferenceScreenStyle">
    <item name="android:textColorPrimary">@color/primary_text_color_selector</item>
    <item name="android:textColorSecondary">@color/secondary_text_color_selector</item>
</style>

in SettingsFragmentActivity.java

public class SettingsFragmentActivity extends PreferenceFragment {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
        PreferenceScreen preferenceScreen = getPreferenceScreen();

        mContext = preferenceScreen.getContext();
        mContext.setTheme(R.style.AppTheme_PreferenceScreenStyle);

        ...
    }
}

This way, Title and Summary are grayed out when option is disabled.

I'm just sharing this solution since I have same problem and it may help someone




回答2:


I figured this out more or less by accident, but if you subclass Preference and override the onBindView(), you can achieve the "grayed out" effect when a preference is disabled:

@Override
protected void onBindView(View view) {
    // TODO Auto-generated method stub
    super.onBindView(view);
    TextView title = (TextView)view.findViewById(android.R.id.title);
    TextView summary = (TextView)view.findViewById(android.R.id.summary);

    if (title.isEnabled()) {
        title.setTextColor(getContext().getResources().getColor(R.color.gold));
    }
    else {
        title.setTextColor(getContext().getResources().getColor(R.color.gold_disabled));
    }

    if (summary.isEnabled()) {
        summary.setTextColor(getContext().getResources().getColor(R.color.orange));
    }
    else {
        summary.setTextColor(getContext().getResources().getColor(R.color.orange_disabled));
    }
}


来源:https://stackoverflow.com/questions/17769198/android-change-color-of-disabled-text-using-theme-style

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