如何设置Android的偏好摘要文本颜色?
-
25-09-2019 - |
题
在我的个人偏好屏幕我有一个偏好,单击时打开拾色器对话框。我想要做的是,当用户选择颜色,该偏好的文本摘要显示在该颜色
我知道可以有汇总设置像这样,Currently <font color="#ff0000">this color</font>
并将它在彩色显示。问题是我找回的颜色是Android INT颜色。
我可以使用红色(),绿色(),蓝色()方法,然后转换那些为十六进制,然后将它们组合成一个字符串,这样我就可以用新值的摘要文本和作品:String colorString = String.format("#%02x%02x%02x",Color.red( defaultColor ), Color.green( defaultColor ), Color.blue( defaultColor ));
我只是好奇,如果有一个更简单的方法来做到这一点。
由于时间提前。
肖恩
解决方案
OK我落得这样做使用Spannable。这需要的颜色为整数。
Spannable summary = new SpannableString("Currently This Color");
summary.setSpan(new ForegroundColorSpan(color), 0, summary.length(), 0);
preference.setSummary(summary);
其他提示
使用Html.fromHtml样式文本。
mPodFolderPref.setTitle(Html.fromHtml("<font color='red'>" + mPodFolderPref.getTitle() + "</font>"));
mPodFolderPref.setSummary(Html.fromHtml("<font color='red'>" + mPodFolderPref.getSummary() + "</font>"));
Html.fromHtml能为你做了很多。
晚了一些,但我发现写这些自包含方法中有用的:
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);
}
以上所有方式对我没有帮助。我结束了由扩展Prefernces类:
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));
}
}
您好,您可以使用改变偏爱的颜色Html.fromHtml()。
例如:私人字符串标题= “” + “设置SMS发送限制” + “”;
和您这样的Android应用程序中添加设置该字符串。
CheckBoxPreference _test =(CheckBoxPreference)findPreference( “文本”); _test .setTitle(Html.fromHtml(标题));
遵循此链接的机器人HTML视图: http://www.androidpeople.com/tag/html-tags/
由于