Android:使用主题/样式更改禁用文本的颜色? [英] Android: change color of disabled text using Theme/Style?

查看:952
本文介绍了Android:使用主题/样式更改禁用文本的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在color.xml中定义了以下颜色:

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

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

和以下主题:

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



在我的SettingsActivity中,我有一个CheckBoxPreference和一个取决于它的Preference。当CheckBoxPreference未被选中时,首选项被禁用,但是,由于我设置的自定义金色文本颜色,它不会像默认颜色一样灰色。如何在XML中更改此?我尝试设置:

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>

但没有任何效果。

推荐答案

我偶然想出来了,但如果你的子类化Preference并覆盖onBindView(),你可以实现灰色的效果,当首选项被禁用:

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));
    }
}

这篇关于Android:使用主题/样式更改禁用文本的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆