如何设置 Android 首选项摘要文本颜色?
在我的首选项屏幕上,我有一个首选项,单击时会打开一个颜色选择器对话框。我想做的是,当用户选择一种颜色时,首选项的文本摘要以该颜色显示。
我知道我可以像这样设置摘要,当前 此颜色
并以该颜色显示。问题是我返回的颜色是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我最终做的是使用 Spannable。这将颜色视为整数。
OK what I ended up doing was using a Spannable. This takes the color as an integer.
使用 Html.fromHtml 来设置文本样式。
Html.fromHtml 可以为您做很多事情。
Use Html.fromHtml to style your text.
Html.fromHtml can do a lot for you.
有点晚了,但我发现编写这些独立的方法很有用:
A bit late, but I found useful to write these self-contained methods:
以上所有方法都没有帮助我。我最终扩展了 Preferences 类:
All the above ways didn't help me. I ended up by extends the Prefernces class:
您好,您可以使用 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