2013-10-19 37 views
20

我嘗試了幾件事情,並沒有什麼工作...我試圖改變BACKGROUNDCOLOR Android上的一個ImageView的,但沒有任何反應......如何在Android上的ImageView上設置BackgroundColor?

這裏是我的xml:

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="350dp" 
    android:layout_height="550dp" 
    android:layout_above="@+id/btnInfo" 
    android:layout_alignLeft="@+id/fundo" 
    android:layout_alignRight="@+id/btnInfo" 
    android:layout_alignTop="@+id/fundo" 
    android:layout_centerHorizontal="true" 
    android:contentDescription="@string/backgroundMain" /> 

和代碼:

public void onStart() 
    { 
     super.onStart(); 
     Log.d("Teste", "In the onStart() event 5"); 

     ImageView backgroundImg = (ImageView) findViewById(R.id.imageView1); 
     backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255)); 
    } 

我錯過了什麼?

+2

也許如果我設置了不同的顏色,而不是白色,我可以看到它......¬¬¬,我的代碼正在工作......感謝誰讀那個;) – CarinaPilar

+2

我要指出,但我認爲不可能! –

回答

20

RGB:255,255,255是WHITE的顏色代碼。由於您的父級佈局背景顏色也是白色的,因此您不會看到區別。像

backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50)); 

否則

嘗試改變顏色改變父佈局的背景顏色。

+0

是啊,真是太遺憾了......當我意識到這是白色問題時,太遲了......謝謝! ;-) – CarinaPilar

+1

是的..有時這些愚蠢的錯誤發生..歡迎:) –

+0

是否有可能在xml文件上設置?用背景圖像改變背景顏色? –

16

理論上它應該工作...但嘗試這樣的:

backgroundImg.setBackgroundColor(Color.parseColor("#FFFFFF")); 
15

沒有什麼錯與您的代碼。但我更喜歡通過xml來做到這一點,這也將解決您的問題。只需在ImageView標籤中添加此項即可。

android:background="@android:color/black" 
1

如果你想使用放置在繪製文件夾中的XML文件,你可能需要使用:

imageView.setBackgroundResource(R.drawable.drawable); 
相關問題