【问题标题】:how to get color from colors.xml and set it in strings.xml file如何从 colors.xml 中获取颜色并将其设置在 strings.xml 文件中
【发布时间】:2021-06-25 06:04:14
【问题描述】:

我在strings.xml 文件中有一个字符串。我想为字符串的特定部分设置颜色。这是代码。

<string name="welcome_name">Welcome to <font color='#00adb5'>Home</font> </string>

效果很好。但我不想直接在字符串中设置颜色。我想通过colors.xml文件设置它

如下所示:

<string name="welcome_name">Welcome to <font color='@color/special_text_color'>Home</font> </string>

它不起作用。如何使它工作。请帮我解决一些问题。

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    在纯 XML 中,这永远不会起作用。这取决于您如何使用该 XML。 那么您希望哪个软件能够理解“@color/special_text_color”? 您使用哪个软件将 XML 呈现给用户?

    【讨论】:

      【解决方案2】:

      您可以使用 Spannble 属性为字符串中的特定文本着色,而不是在 string.xml 中设置颜色

      TextView TV = (TextView)findViewById(R.id.mytextview01);
      Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
      
      wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      
      TV.setText(wordtoSpan);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        • 2018-09-25
        • 1970-01-01
        • 2010-09-27
        相关资源
        最近更新 更多