How to set the text color of TextView in code?

后端 未结 30 2576
你的背包
你的背包 2020-11-22 07:48

In XML, we can set a text color by the textColor attribute, like android:textColor=\"#FF0000\". But how do I change it by coding?

I tried s

相关标签:
30条回答
  • 2020-11-22 08:30

    You should use:

    holder.text.setTextColor(Color.RED);
    

    You can use various functions from the Color class to get the same effect of course.

    • Color.parseColor (Manual) (like LEX uses)

      text.setTextColor(Color.parseColor("#FFFFFF"));
      
    • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

      holder.text.setTextColor(Color.rgb(200,0,0));
      holder.text.setTextColor(Color.argb(0,200,0,0));
      
    • And of course, if you want to define your color in an XML file, you can do this:

      <color name="errorColor">#f00</color>
      

      because the getColor() function is deprecated1, you need to use it like so:

      ContextCompat.getColor(context, R.color.your_color);
      
    • You can also insert plain HEX, like so:

      myTextView.setTextColor(0xAARRGGBB);
      

      Where you have an alpha-channel first, then the color value.

    Check out the complete manual of course, public class Color extends Object.


    1This code used to be in here as well:

    textView.setTextColor(getResources().getColor(R.color.errorColor));
    

    This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.

    0 讨论(0)
  • 2020-11-22 08:32

    From API 23 onward, getResources().getColor() is deprecated.

    Use this instead:

    textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.color_black));
    
    0 讨论(0)
  • 2020-11-22 08:33

    If you still want to specify your colors in your XML file:

    <color name="errorColor">#f00</color>
    

    Then reference it in your code with one of these two methods:

    textView.setTextColor(getResources().getColor(R.color.errorColor, getResources().newTheme()));    
    

    or

    textView.setTextColor(getResources().getColor(R.color.errorColor, null));
    

    The first is probably preferable if you're compiling against Android M, however the theme you pass in can be null, so maybe that's easier for you?

    And if you're using the Compat library you can do something like this

    textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor));
    
    0 讨论(0)
  • 2020-11-22 08:33

    Similarly, I was using color.xml:

    <color name="white">#ffffff</color>
        <color name="black">#000000</color>   
    

    For setting the TextView background like:

    textView.setTextColor(R.color.white);
    

    I was getting a different color, but when I used the below code I got the actual color.

    textView.setTextColor(Color.parseColor("#ff6363"));
    
    0 讨论(0)
  • 2020-11-22 08:35

    I believe that if you want to specify a color as a resource (in the XML file), you'll have to provide its ARGB value (not simply the RGB value).

    Try changing your color value to #FFFF0000. It should give you RED.

    0 讨论(0)
  • 2020-11-22 08:36
    holder.text.setTextColor(Color.rgb(200,0,0));
    

    or

    myTextView.setTextColor(0xAARRGGBB);
    
    0 讨论(0)
提交回复
热议问题