Android記錄一個setTextColor常見的一個bug,androidbug
今天寫代碼 一不小心就犯了個錯誤。仔細檢查才發現,僅記錄一下,防止各位同學犯同樣的錯誤哦
代碼如下:
remote.setTextColor(summaryId, R.color.news_have_read);
如上,代碼無論如何都不能正常顯示顏色。只好換成如下代碼:
remote.setTextColor(summaryId,0xfdfdfdfd);
總不能寫顏色值多麻煩。。還是寫的color.xml中吧。
最後才發現應該這樣寫才最對。
remote.setTextColor(summaryId, mContext.getResources().getColor(R.color.news_is_have_read));
在remote.setTextColor(summaryId, R.color.news_have_read);這段代碼中,顯示的是R.color.news_have_read通過eclipse產生的int值並不是真正的顏色值,必須通過mContext.getResources().getColor將真正的16進位顏色值#255#255#255#255值取得到才可以。
Android中要顯示一個表格,是這樣的:
TableLayout t;TableRow row;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);t = (TableLayout) findViewById(R.id.t);int z[] = {4,3,5};ArrayList<View> v = new ArrayList<View>();for (int x = 0; x < 3; x++) {row = new TableRow(this);for (int i = 0; i < z[x]; i++) {TextView t1 = new TextView(this);t1.setText("32323232");t1.setTextColor(Color.BLACK);row.addView(t1);}t.addView(row);}}
android的開發,在環境裡的res/Values檔案夾裡沒有一個Color的xml檔案,於是我建立了一個問題就來了
Resources.getColor()看看你的引用路徑對嗎?
android.blog.51cto.com/268543/302529給你推薦一個部落格專門講解資源檔夾的。