【问题标题】:How can I set the android preference summary text color?如何设置 android 偏好摘要文本颜色?
【发布时间】:2011-04-29 15:14:16
【问题描述】:

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

我知道我可以这样设置摘要,Currently <font color="#ff0000">this color</font> 并以该颜色显示。问题是我返回的颜色是 android int 颜色。

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

提前谢谢。

肖恩

【问题讨论】:

    标签: android text colors summary preference


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 不知道为什么它被否决,但这是对问题最简单的答案,也最容易实施。
      • .fromHtml 解决方案还允许您更改许多属性,而不仅仅是文本颜色,例如 将文本加粗。因此,这对我来说是更有价值的答案。赞成。
      • 它已经折旧了。
      • 这可行,但我认为它不适用于 ListPreference
      【解决方案3】:

      有点晚了,但我发现编写这些自包含方法很有用:

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

      【讨论】:

        【解决方案4】:

        以上所有方法都没有帮助我。我最终扩展了 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));
            }
        
        }
        

        【讨论】:

          【解决方案5】:

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

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

          然后像这样从你的 android 应用程序中添加设置这个字符串。

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

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

          谢谢

          【讨论】:

          • 我知道这一点。但是,就像我上面所说的,我可以将颜色作为 int 访问,这就是 Android 存储它的方式。不是 RGB 或 ARGB 字符串。
          猜你喜欢
          • 2011-10-07
          • 1970-01-01
          • 2012-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-11
          • 1970-01-01
          相关资源
          最近更新 更多