How to Set BackgroundColor on ImageView on Android?

不打扰是莪最后的温柔 提交于 2020-01-01 01:11:26

问题


I tried a couple things and nothing is working... I'm trying to change the BackgroundColor on a ImageView on Android, but nothing happens...

Here is my 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" />

And the code:

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));
    }

What am I missing?


回答1:


RGB:255, 255, 255 is the color code for WHITE. Since your parent layout background color is also white you won't see the difference.

Try changing color like

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

Or else change the background color of parent layout.




回答2:


There's nothing wrong with your code. But I would prefer doing this through xml , This will also solve your problem. Just add this in your ImageView tag.

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



回答3:


In theory it should work...but try like this:

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



回答4:


If you want to use a XML file placed in drawable folder, you might need to use:

imageView.setBackgroundResource(R.drawable.drawable);



回答5:


Using PorterDuff.Mode:

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

PorterDuff.mode gives a way of composing and overlaying images in Android, see also What does PorterDuff.Mode mean in android graphics.What does it do?



来源:https://stackoverflow.com/questions/19461191/how-to-set-backgroundcolor-on-imageview-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!