How to set the text color of TextView in code?

后端 未结 30 2972
你的背包
你的背包 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:28

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

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

      #800080
      

      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));
      

提交回复
热议问题