How to set the text color of TextView in code?

后端 未结 30 2627
你的背包
你的背包 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:16
    textView.setTextColor(ContextCompat.getColor(getApplicationC‌​ontext(),R.color.col‌​orWhite)); 
    

    In the colors.xml file, write in the code below:

    <color name="colorWhite">#FFFFFF</color>
    
    0 讨论(0)
  • 2020-11-22 08:16
    text1.setTextColor(Color.parseColor("#000000"));
    
    0 讨论(0)
  • 2020-11-22 08:16

    if you want to give color code directly then use

    textView.setTextColor(Color.parseColor("#ffffff"));
    

    or if you want to give color code from colors folder then use

    textView.setTextColor(R.color.white);
    
    0 讨论(0)
  • 2020-11-22 08:16

    Try this:

    TextView textview = (TextView) findViewById(R.id.textview );
    textview .setTextColor(Color.parseColor("#85F85F"));
    
    0 讨论(0)
  • 2020-11-22 08:20

    Using Adapter you can set the text color by using this code:

    holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
    holder.text_view.setTextColor(Color.parseColor("#FF00FF"));
    
    0 讨论(0)
  • 2020-11-22 08:21

    For providing rgb values: text.setTextColor(Color.rgb(200,0,0));
    For parsing the color from a hex value: text.setTextColor(Color.parseColor("#FFFFFF"));

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