How to set the text color of TextView in code?

后端 未结 30 2578
你的背包
你的背包 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:23
    holder.userType.setTextColor(context.getResources().getColor(
                        R.color.green));
    
    0 讨论(0)
  • 2020-11-22 08:26

    If you are in an adapter and still want to use a color defined in resources you can try the following approach:

    holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));
    
    0 讨论(0)
  • 2020-11-22 08:27

    You can do this only from an XML file too.

    Create a color.xml file in the values folder:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="textbody">#ffcc33</color>
    
    </resources>
    

    Then in any XML file, you can set color for text using,

    android:textColor="@color/textbody"
    

    Or you can use this color in a Java file:

    final TextView tvchange12 = (TextView) findViewById(R.id.textView2);
    //Set color for textbody from color.xml file
    tvchange1.setTextColor(getResources().getColor(R.color.textbody));
    
    0 讨论(0)
  • 2020-11-22 08:28

    There are many different ways to set color on text view.

    1. Add color value in studio res->values->colors.xml as

      <color name="color_purple">#800080</color>
      

      Now set the color in xml or actvity class as

      text.setTextColor(getResources().getColor(R.color.color_purple)
      
    2. If you want to give color code directly use below Color.parseColor code

      textView.setTextColor(Color.parseColor("#ffffff"));   
      
    3. You can also use RGB

      text.setTextColor(Color.rgb(200,0,0));
      
    4. Use can also use direct hexcode for textView. You can also insert plain HEX, like so:

      text.setTextColor(0xAARRGGBB);
      
    5. You can also use argb with alpha values.

         text.setTextColor(Color.argb(0,200,0,0));
      

      a for Alpha (Transparent) v.

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

         text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
      
    0 讨论(0)
  • 2020-11-22 08:28

    I normally do this for any views:

    myTextView.setTextColor(0xAARRGGBB);
    

    where

    • AA defines alpha (00 for transparent, FF for opaque)

    • RRGGBB defines the normal HTML color code (like FF0000 for red).

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

    Kotlin Extension Solution

    Add these to make changing text color simpler

    For setting ColorInt

    myView.textColor = Color.BLACK // or Color.parseColor("#000000"), etc.
    
    var TextView.textColor: Int
    get() = currentTextColor
    set(@ColorInt color) {
        setTextColor(color)
    }
    

    For setting ColorRes

    myView.setTextColorRes(R.color.my_color)
    
    fun TextView.setTextColorRes(@ColorRes colorRes: Int) {
        val color = ContextCompat.getColor(context, colorRes)
        setTextColor(color)
    }
    
    0 讨论(0)
提交回复
热议问题