如何设置 Android 首选项摘要文本颜色?

发布于 2024-09-28 21:42:39 字数 460 浏览 4 评论 0原文

在我的首选项屏幕上,我有一个首选项,单击时会打开一个颜色选择器对话框。我想做的是,当用户选择一种颜色时,首选项的文本摘要以该颜色显示。

我知道我可以像这样设置摘要,当前 此颜色 并以该颜色显示。问题是我返回的颜色是 android int 颜色。

我可以使用 red()、green()、blue() 方法,然后将它们转换为十六进制,然后将它们组合成一个字符串,这样我就可以使用新值设置摘要文本,这样就可以工作:String colorString = String.format("#%02x%02x%02x",Color.red(defaultColor), Color.green(defaultColor), Color.blue(defaultColor)); 我只是好奇是否有更简单的方法来做到这一点。

提前致谢。

肖恩

On my preference screen I have a preference that when clicked opens a color picker dialog. What I would like to do is when the user selects a color, that the text summary of the preference is displayed in that color.

I know I can have the summary set up like this, Currently <font color="#ff0000">this color</font> and have it display in that color. The problem is the color I am getting back is the android int color.

I could use the red(), green(), blue() methods and then convert those to Hex and then combine them into a string so I could set the summary text with the new value and that works: String colorString = String.format("#%02x%02x%02x",Color.red( defaultColor ), Color.green( defaultColor ), Color.blue( defaultColor )); I was just curious if there is an easier way to do this.

Thanks ahead of time.

Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲欢浪云 2024-10-05 21:42:39

好吧,我最终做的是使用 Spannable。这将颜色视为整数。

Spannable summary = new SpannableString("Currently This Color");
summary.setSpan(new ForegroundColorSpan(color), 0, summary.length(), 0);
preference.setSummary(summary);

OK what I ended up doing was using a Spannable. This takes the color as an integer.

Spannable summary = new SpannableString("Currently This Color");
summary.setSpan(new ForegroundColorSpan(color), 0, summary.length(), 0);
preference.setSummary(summary);
烟火散人牵绊 2024-10-05 21:42:39

使用 Html.fromHtml 来设置文本样式。

mPodFolderPref.setTitle(Html.fromHtml("<font color='red'>" +  mPodFolderPref.getTitle() + "</font>"));
mPodFolderPref.setSummary(Html.fromHtml("<font color='red'>" +  mPodFolderPref.getSummary() + "</font>"));

Html.fromHtml 可以为您做很多事情。

Use Html.fromHtml to style your text.

mPodFolderPref.setTitle(Html.fromHtml("<font color='red'>" +  mPodFolderPref.getTitle() + "</font>"));
mPodFolderPref.setSummary(Html.fromHtml("<font color='red'>" +  mPodFolderPref.getSummary() + "</font>"));

Html.fromHtml can do a lot for you.

尘曦 2024-10-05 21:42:39

有点晚了,但我发现编写这些独立的方法很有用:

private void setColorPreferencesTitle(EditTextPreference textPref, int color) {
    CharSequence cs     = (CharSequence) textPref.getTitle();
    String plainTitle   = cs.subSequence(0, cs.length()).toString();
    Spannable coloredTitle = new SpannableString (plainTitle);
    coloredTitle.setSpan( new ForegroundColorSpan(color), 0, coloredTitle.length(), 0 );
    textPref.setTitle(coloredTitle);
}

private void resetColorPreferencesTitle(EditTextPreference textPref) {
    CharSequence cs     = (CharSequence) textPref.getTitle();
    String plainTitle   = cs.subSequence(0, cs.length()).toString();
    textPref.setTitle(plainTitle);
}

A bit late, but I found useful to write these self-contained methods:

private void setColorPreferencesTitle(EditTextPreference textPref, int color) {
    CharSequence cs     = (CharSequence) textPref.getTitle();
    String plainTitle   = cs.subSequence(0, cs.length()).toString();
    Spannable coloredTitle = new SpannableString (plainTitle);
    coloredTitle.setSpan( new ForegroundColorSpan(color), 0, coloredTitle.length(), 0 );
    textPref.setTitle(coloredTitle);
}

private void resetColorPreferencesTitle(EditTextPreference textPref) {
    CharSequence cs     = (CharSequence) textPref.getTitle();
    String plainTitle   = cs.subSequence(0, cs.length()).toString();
    textPref.setTitle(plainTitle);
}
不羁少年 2024-10-05 21:42:39

以上所有方法都没有帮助我。我最终扩展了 Preferences 类:

public class CustomListPreferences extends Preference {

    public CustomListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomListPreferences(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
       ((TextView)view.findViewById(android.R.id.summary)).setTextColor(getContext().get Resources().getColor(R.color.green));
    }

}

All the above ways didn't help me. I ended up by extends the Prefernces class:

public class CustomListPreferences extends Preference {

    public CustomListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomListPreferences(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
       ((TextView)view.findViewById(android.R.id.summary)).setTextColor(getContext().get Resources().getColor(R.color.green));
    }

}
荒人说梦 2024-10-05 21:42:39

您好,您可以使用 Html.fromHtml() 更改首选颜色。

ex : private String title = "" + "设置短信发送限制" + "";

并从您的 Android 应用程序中添加设置此字符串,如下所示。

CheckBoxPreference _test = (CheckBoxPreference)findPreference(“文本”);
_test .setTitle(Html.fromHtml(标题 ));

点击此链接查看 android 的 html 视图:
http://www.androidpeople.com/tag/html-tags/

谢谢

Hi you can change the color of preference using Html.fromHtml().

ex : private String title = "" + "Set the SMS Send Limit" + "";

and add set this string from your android application like this .

CheckBoxPreference _test = (CheckBoxPreference)findPreference("text");
_test .setTitle(Html.fromHtml(title ));

follow this link for html view of android :
http://www.androidpeople.com/tag/html-tags/

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文