Is it possible to set the color of a string directly in string.xml?

前端 未结 12 1030
轮回少年
轮回少年 2021-02-07 12:17

I mean something like:

Error!
12条回答
  •  别那么骄傲
    2021-02-07 12:47

    You can change text color through string.xml :

    Completed / Running / Not Alloted
    

    If you want to set text programatically then you can use it it will render your text color dynamically.

     private SpannableStringBuilder setSpan(int completed, int running){
                SpannableStringBuilder span1 = new SpannableStringBuilder(completed+" / ");
                ForegroundColorSpan color1=new ForegroundColorSpan(Color.parseColor("#6DC287"));
                span1.setSpan(color1, 0, span1.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    
                SpannableStringBuilder span2 = new SpannableStringBuilder(running+"");
                ForegroundColorSpan color2=new ForegroundColorSpan(Color.parseColor("#FFCC29"));
                span2.setSpan(color2, 0, span2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    
                Spanned concatenated=(Spanned) TextUtils.concat(span1, span2);
    
                return new SpannableStringBuilder(concatenated);
            }
    

提交回复
热议问题