没有图像资源时,无法设置图像视图的背景颜色

Ala*_*ita 4 android setbackground android-imageview

我想为ImageView设置背景颜色,当imageview有图像资源时,它可以工作,我得到我的背景颜色但是当图像视图没有图像资源时,SetBackgroundColor不起作用,什么都不做,这是我的代码:

xml布局:

       <ImageView android:layout_centerHorizontal="true"
        android:id="@+id/favor_item_image" 
        android:layout_alignParentTop="true" android:layout_height="wrap_content"
        android:layout_width="match_parent" android:adjustViewBounds="true"/>
Run Code Online (Sandbox Code Playgroud)

java代码:

       ImageView favorImage = (ImageView) MyView.findViewById(R.id.favor_item_image);  
       //favorImage.setImageResource(R.drawable.btndelete);
       favorImage.setBackgroundColor(Color.argb(255,255,0,0));
Run Code Online (Sandbox Code Playgroud)

所以,当我评论这一行://favorImage.setImageResource(R.drawable.btndelete); 我无法设置图像视图背景颜色,但是当我取消注释这一行时,setBackgroundColor工作正常.

请帮忙.谢谢

Bob*_*ke4 6

您没有看到任何内容的原因是因为您将layout_height设置为wrap_content.当你没有.如果你想要的颜色显示没有你必须将layout_height设置为实际值如50dp图像WRAP_CONTENT设置高度为0的ImageView的图片集.