【问题标题】:Efficient way to change the particular text in particular color in strings.xml?在strings.xml中以特定颜色更改特定文本的有效方法?
【发布时间】:2016-12-31 17:42:35
【问题描述】:

我为 Android 技术词设计了一个Dictionary App..它几乎完成了..现在我需要设置视图样式..所以我决定为关键字代码提供颜色..在我的 String.xml 中,我有 99+ string classes..

我曾经像下面提到的代码那样更改字体颜色..此代码有效

  <string name="xml_code">
    <![CDATA[<b><font color=#000080>&lt;LinearLayout</b><br><br>
            \t\t <b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_width=</b><b><font color=#008000>\"match_parent\"</b><br><br>
            \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_height=</b>><b><font color=#008000>\"match_parent\"</b><br><br>
            \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:orientation=</b><b><font color=#008000>\"vertical\"</b>&gt;<br><br>
    <b><font color=#000080>&lt;TextView</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_width=</b><b><font color=#008000>\"match_parent\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_height=</b><b><font color=#008000>\"wrap_content\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:text=</b><b><font color=#008000>\"Hello\"</b>/&gt;<br><br>
    <b><font color=#000080>&lt;ImageView</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_width=</b><b><font color=#008000>\"match_parent\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_height=</b><b><font color=#008000>\"wrap_content\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:src=</b><b><font color=#008000>\"@drawable/mountain\"</b>/&gt;<br><br>
    <b><font color=#000080>&lt;Button</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_width=</b><b><font color=#008000>\"match_parent\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:layout_height=</b><b><font color=#008000>\"wrap_content\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:text=</b><b><font color=#008000>\"Press me\"</b><br><br>
        \t\t<b><font color=#660E7A>android</b><b><font color=#0000FF>:onClick=</b><b><font color=#008000>\"doSomething\"</b>/&gt;<br><br>
    <b><font color=#000080>&lt;/LinearLayout&gt;</b>]]>
    </string>

在整个字符串中,

if word is android,it should be violet
if word is layout:width/etc.. ,it should blue
if word is values,it should be green..

对于我上面提到的代码,输出是

我需要我的输出,如屏幕截图..它可以从上面的代码中工作..任何人都可以说如何 minimize the code to change the font Color..因为我需要更改 99+ 字符串类的字体颜色。 .请给我一个最简单的方法来改变我的字体颜色..如果你不明白我的问题,请问我..

【问题讨论】:

  • 使用ForegroundColorSpan 和一些Spannable
  • 你向谷歌叔叔问过:ForegroundColorSpan Spannable吗?注意ForegroundColorSpan,而不是ForegroundSpan
  • 我搜索 ForegroundSpan 和 Spannable..但我这样问..考虑单个字符串“Android 应用程序开发”..在这个字符串中,Android 应该是红色,然后应用程序应该是蓝色,然后发展应该是绿色..现在你明白了吗
  • 不是ForegroundSpan!搜索ForegroundColorSpan
  • 好的,我不是这样的..请查看我的屏幕截图..在我的屏幕截图中,整个字符串在单个类下..在类中我必须将不同的字体颜色更改为不同的单词

标签: android xml fonts


【解决方案1】:

希望它有效..

SpannableString styledString = "Ajay";
styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 1, 0);
  // Give the styled string to a TextView
  TextView textView = new TextView(this);
  textView.setMovementMethod(LinkMovementMethod.getInstance());
  textView.setText(styledString);

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 2016-02-02
    • 2019-08-10
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多