且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:使用主题/样式更改禁用文本的颜色?

更新时间:2023-01-27 20:13:22

我偶然想出来了,但如果你的子类化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));
    }
}