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

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

I mean something like:

Error!
12条回答
  •  一个人的身影
    2021-02-07 12:46

    i hope this help for you.

       TextView textView=findViewById(R.id.text);
    
       String error= getResources().getString(R.string.error);
       ForegroundColorSpan fgColor=new ForegroundColorSpan(Color.RED);  // here set the color of Text
    
       SpannableString ss=new SpannableString(error);  //here set the text to spannableString
    
       ss.setSpan(fgColor,0,error.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //here which color , when to where you will set the color
       textView.setText(ss);
    

    What is SpannableString in android ?

    Android SpannableString Example. The SpannableString in android is an excellent way to style strings in a TextView. Put simply, it allows a TextView to provide different styles to different areas of text.

提交回复
热议问题