如何在Android上的ImageView上设置BackgroundColor?

问题描述:

我尝试了几件事情,并没有什么工作...我试图改变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

我要指出,但我认为不可能! –

RGB:255,255,255是WHITE的颜色代码。由于您的父级布局背景颜色也是白色的,因此您不会看到区别。像

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

否则

尝试改变颜色改变父布局的背景颜色。

+0

是啊,真是太遗憾了......当我意识到这是白色问题时,太迟了......谢谢! ;-) – CarinaPilar

+1

是的..有时这些愚蠢的错误发生..欢迎:) –

+0

是否有可能在xml文件上设置?用背景图像改变背景颜色? –

理论上它应该工作...但尝试这样的:

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

没有什么错与您的代码。但我更喜欢通过xml来做到这一点,这也将解决您的问题。只需在ImageView标签中添加此项即可。

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

如果你想使用放置在绘制文件夹中的XML文件,你可能需要使用:

imageView.setBackgroundResource(R.drawable.drawable); 

使用PorterDuff.Mode

imageView1.setColorFilter(colorCode,android.graphics.PorterDuff.Mode.SRC_IN); 

PorterDuff.mode给出了创作和重叠图像的方式Android,另请参阅What does PorterDuff.Mode mean in android graphics.What does it do?