2012-03-20 80 views
0

我在更改android中的顏色時遇到問題。 我有,我用添加元素的列表:Android和顏色

colorsPictures.add(Color.BLUE); 
colorsPictures.add(Color.YELLOW); 
colorsPictures.add(Color.BLACK); 

在的onCreate()我實現了一些類似這樣的:

pictureOne.setImageResource(colorsPictures.get(0))); 
pictureTwo.setImageResource(colorsPictures.get(1)); 

pictureOne和pictureTwo是:

pictureOne = (ImageView) findViewById(R.id.imageViewPicture1); 
    pictureTwo = (ImageView) findViewById(R.id.imageViewPicture2); 

我想當我開始活動時,我的兩個imageview都會在藍色和黃色上改變顏色。我認爲我在添加到我的列表中有一個錯誤。我知道我可以改變佈局的顏色,但我必須這樣做,這樣。幫幫我吧。

例如: 我在佈局上有兩個imageViews。他們有白色的顏色。我想在運行活動時更改顏色,然後單擊按鈕。 OnClick偵聽器已實現,但當我嘗試更改顏色時,我得到了nullpointexception。

+0

如果您在圖像視圖的背景中有圖像,則更改顏色不會顯示任何背景,它只會顯示背景圖像。有關詳細信息[請閱讀此文件](http://konsentia.com/2011/03 /動態變化的最背景色功能於機器人的小部件/) – Sameer 2012-03-20 09:25:04

回答

1

問題是,您正在使用setImageResource(int resId),方法參數是可繪製資源,而不是顏色。如果你看看你的日誌,你可能會看到一個警告或異常,說沒有找到資源。

可以代替使用這樣的:

pictureOne.setBackgroundColor(colorsPictures.get(0))); 
pictureTwo.setBackgroundColor(colorsPictures.get(1)); 
2

如果你唯一的目標是改變背景顏色你爲什麼不只是做,

pictureone.setBackgroundResource(colorsPicture.get(0)); 
1

參數setImageResource的是資源ID。你正在把它的顏色。用途:

pictureOne.setBackgroundColor(colorsPictures.get(0))); 

或定義yoyr在colors.xml和使用顏色:

pictureTwo.setImageResource(R.color.YOUR_COLOR_ID);

0

您設置在動態,那麼你必須做like..try這樣的..

setTextColor(getContext().getResources().getColor(R.color.info_button_text_color)); 

希望這可以幫助你。

0

setImageResource用於設置任何圖像其可在抽拉文件夾,如下

imageView.setImageResource(R.drawable.myimage); 

您正在使用此方法來設置顏色與直接價值。它會給出未知的結果,因爲在運行時,顏色值將被視爲資源ID。