Set Text Color for textView Android

后端 未结 8 1846
梦谈多话
梦谈多话 2021-02-06 03:41

In the string.xml file i use the following tag

 \"#0000ff\"

If I use

 textview         


        
相关标签:
8条回答
  • 2021-02-06 03:50

    try set color like this may helps you

    txt.setTextColor(Color.rgb(0, 87, 48));
    

    this is different way but it can change color , here need Red,Green,Blue Code to pass

    0 讨论(0)
  • 2021-02-06 03:51

    You can use

      textView1.setTextColor(getResources().getColor(R.color.mycolor))
    

    or

      textview1.setBackgroundColor(Color.parseColor("#ffffff"));
    

    or

        textview1.setBackgroundColor(Color.RED);
    

    or

        textView1.setBackgroundColor(R.color.black);
    
    0 讨论(0)
  • 2021-02-06 03:52

    you should use R.color.CodeColor. you are using R.string.CodeColor.

    0 讨论(0)
  • 2021-02-06 03:53

    I am basically just merging all the partially good answers.

    You defined your color as a String, but AFAIK Android processes colors as Itegers.
    So use the Colors.xml file (instead of strings.xml): and refer to it in code as R.color.CodeColor.
    (Moreover, I think, there is some naming convention that tells you to name these values all lowercase: code_color or codecolor)

    Or you can define them as strings, but then you are need to make it an Integer: Color.parseColor(R.string.code_color).

    0 讨论(0)
  • 2021-02-06 03:55

    Define colors in colors.xml file like that:

      <resources>
           <color name="CodeColor" >#0000ff</color>
      </resources>
    

    Then use color whatever you like in your code using: R.color.CodeColor

    Good luck!

    0 讨论(0)
  • 2021-02-06 03:55

    I tried something like:

    textView.setTextColor(R.color.Red);

    0 讨论(0)
提交回复
热议问题