关于Android中控件颜色的设置一般有四种方法:
以设置tv中文本颜色为例:
一、使用系统提供的颜色:
tv.setTextColor(Color.RED)
Color是android.graphics包下的。
如果在xml中使用系统提供的颜色,可以直接配置TextView的textColor属性:
android:textColor="@android:color/white"
二、直接使用数字表示颜色
tv.setTextColor(0xffff0000);
其中,一共八位,前两位表示透明度,从00到ff即从完全透明到不透明;后面六位表示颜色,每两位一组分别为红绿蓝
三、在资源文件中,配置colors.xml,在该文件中写颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 常用颜色值 -->
<color name="white40">#40FFFFFF</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="gray">#808080</color>
<color name="red">#FF0000</color>
<color name="gold">#FFD700</color>
<color name="yellow">#FFFF00</color>
<color name="green">#008000</color>
</resources>
使用时直接
tv.setTextColor(getResources().getColor(R.color.red));
colors.xml文件中color标签也可以使用drawable代替
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
使用时直接
tv.setTextColor(tx.getResources().getColor(R.drawable.red));
四、可以在xml中直接配置TextView文本颜色
android:textColor="#F8F8FF00"
透明度也可以不写,即
android:textColor="#F8FF00"
注意加上#