2011-12-13 106 views
4

如何將顏色代碼以整數ex:13369395轉換爲android專用。由於13369395也是我試過的整數如何使用整數設置顏色?

mainLayout.setTextColor(13369395); 

但它不工作。

我也試圖轉換13369395爲十六進制,如:

mainLayout.setBackgroundColor(Integer.parseInt(13369395 +"", 16)+0xFF000000); 

,但它也沒有幫助。

回答

-2

可以直接採取十六進制代碼。例如

mainLayout.setBackgroundColor(#0BB5FF);

+0

我得到了解決。只需要使用十六進制,如下所示:Integer.toHexString(color);它返回整數的十六進制字符串,如果你只是通過使用\t mainLayout.setBackgroundColor(Integer.parseInt(hexVal,16));它不會工作。您需要添加掩碼爲\t mainLayout.setBackgroundColor(0xff000000 + Integer.parseInt(hexVal,16));這已經解決了這個問題。謝謝 – amandroid

+0

mainLayout.setBackgroundColor(#0BB5FF);從Java 1.6開始,這不受支持 – LokiDroid

6

我得到了解決方案。只是一個十六進制變通如下:

Integer.toHexString(colour);

它返回十六進制字符串爲您的整數,再如果你只是通過

mainLayout.setBackgroundColor(Integer.parseInt(hexVal,16));

它不會工作使用。您需要添加面具作爲

mainLayout.setBackgroundColor(0xff000000 + Integer.parseInt(hexVal,16));

這已經解決了這個問題

0

的問題是很老了。但我仍然發現這個答案會幫助搜索某種方式的人直接將顏色設置爲整數。

如果你看一下android文檔,white的常數值是-1,黑色是-16777216。 (即)整個顏色的int值範圍是(-1到-16777216)。所以你可以簡單地將整數值加到-16777216。

例如,如果你想設置顏色白色的十進制表示是16777215(0xffffff),那麼16777215 - 16777216會給你-1在android中黑色的顏色常量。

2

嘗試傳:

mainLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));