Android中设置文本颜色的四种方法:
一、利用系统自带的颜色类
tx.setTextColor(android.graphics.Color.RED);
二、数字颜色表示
tx.setTextColor(0xffff00f);
将十六进制颜色代码转换为int类型数值方法:Color.parseColor("#FFFF00") 返回 int 数值 ;
三、自定义颜色
在工程目录values文件夹下新建一个color.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="dkgray">#80808FF0</drawable>
<drawable name="yello">#F8F8FF00</drawable>
<drawable name="white">#FFFFFF</drawable>
<drawable name="darkgray">#938192</drawable>
<drawable name="lightgreen">#7cd12e</drawable>
<drawable name="black">#ff000000</drawable>
<drawable name="blue">#ff0000ff</drawable>
<drawable name="cyan">#ff00ffff</drawable>
<drawable name="gray">#ff888888</drawable>
<drawable name="green">#ff00ff00</drawable>
<drawable name="ltgray">#ffcccccc</drawable>
<drawable name="magenta">#ffff00ff</drawable>
<drawable name="red">#ffff0000</drawable>
<drawable name="transparent">#00000000</drawable>
<drawable name="yellow">#ffffff00</drawable>
</resources>
根据个人需要,颜色可以自行添加。
在Java中设置:
tx.setTextColor(tx.getResources().getColor(R.drawable.red));
color.xml中也可用color标签
<color name="red">#ffff0000</color>
java中设置相应改为:
tx.setTextColor(tx.getResources().getColor(R.color.red));
四、直接在xml的TextView中设置
android:textColor="#F8F8FF00" 或
android:textColor="#F8FF00"