2012-01-17 92 views
1

我想動態更改我的應用程序的字體和背景顏色,但是當我嘗試設置它時,無論選擇什麼顏色都會變成灰色。我已經驗證了顏色是正確的。有任何想法嗎。Android視圖設置文本顏色

mTitleText是EditText上

mTitleText = (EditText) findViewById(R.id.note_edit_title); 
mTitleText.setTextColor(R.color.defaulttextred); 
+0

如何設置的文本?你確定Text中沒有'ColorSpan'嗎? – st0le 2012-01-17 03:50:50

+0

請澄清你想要做什麼? – Piyush 2012-01-17 03:51:06

+0

只是爲了實驗,嘗試'mTitleText.setTextColor(Color.RED);' – st0le 2012-01-17 03:52:31

回答

2

只需使用

mTitleText.setTextColor(getResources().getColorStateList(R.color.defaulttextred)); 
+0

這個工作,爲什麼這個工作,我在做什麼不工作?這是否做了特別的改變?對不起,我是Android編程新手 – kds6253 2012-01-17 04:20:06

+0

它很簡單。我們無法使用R.String.anystring從Strings.xml文件訪問字符串,但要訪問字符串,我們必須編寫整個代碼getResources.getString(R.string.anystring)。類似的情況是顏色。 R.color.colorname只返回在R file.it中指定的顏色ID,不返回顏色。欲瞭解更多詳情,請參閱Android站點 – 2012-01-17 05:48:03

+0

,這是有道理的感謝解釋 – kds6253 2012-01-17 11:27:05