通過TextView的setTextColor方法進行文本顏色的設置,
這里可以有3種方式進行設置: 第1種:tv.setTextColor(android.graphics.Color.RED);//系統自帶的顏色類 第2種:tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的數據,分組一下0x|ff|ff00ff,0x是代表顏色整數的標記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個的顏色表示,不接受ff00ff這種6個的顏色表示。 第3種:tv.setTextColor(this.getResources().getColor(R.color.red));//通過獲得資源文件進行設置。根據不同的情況R.color.red也可以是R.string.red或者R.drawable.red,當然前提是需要在相應的配置文件里做相應的配置,如: <color name="red">#FF0000</color> <drawable name="red">#FF0000</drawable> <string name="red">#FF0000</string>
前面三種是設置固定值,第四種是設置顏色選擇器,當點擊文字時,改變顏色>>>
第4種:動態給文本設置顏色選擇器
textView.setTextColor(getResources().getColorStateList(R.color.selector_title_color));
注意:必須在src目錄新建一個color文件夾,然后再創建一個選擇器,代碼如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000000" android:state_enabled="false"></item>
<item android:color="#ffffff" android:state_enabled="true"></item>
</selector>