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

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

I mean something like:

Error!
12条回答
  •  迷失自我
    2021-02-07 12:35

    As suggested by rekire not possible to set color the way you are doing.

    You can use the method as suggested by rekire.

    In you xml you can specify color for your textview as

      android:textColor="#0EFFFF"
    

    You can also set text color programaticaly

      TextView tv= (TextView)findviewById(R.id.textView1);
      tv.setTextColor(Color.RED);  
    

    To set color of particular words in textview, you can use spannable string

        TextView tv= (TextView)findviewById(R.id.textView1);
        tv.setText("");  
        String s="Hello World";
        SpannableString ss=  new SpannableString(s);                
        ss.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 5, 0);  
        tv.setText(ss);
    

提交回复
热议问题