How to set the text color of TextView in code?

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

    You can do this only from an XML file too.

    Create a color.xml file in the values folder:

    
    
        #ffcc33
    
    
    

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

提交回复
热议问题