【问题标题】:Cannot set the background colour of image view when it don't have an image resource没有图像资源时无法设置图像视图的背景颜色
【发布时间】:2012-04-18 17:48:06
【问题描述】:

我想为 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"/>

java代码:

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

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

请帮忙。 谢谢

【问题讨论】:

    标签: android android-imageview setbackground


    【解决方案1】:

    您没有看到任何内容的原因是您将 layout_height 设置为 wrap_content。当您没有在 ImageView 上设置图像时,wrap_content 将高度设置为 0。如果您希望在没有图像的情况下显示颜色,则必须将 layout_height 设置为实际值,例如 50dp。

    【讨论】:

      【解决方案2】:

      你的ImageView的高度是wrap_content,当你只设置背景颜色时,它是没有内容的。因此不出现。您应该可以通过设置不同的高度来解决此问题。

      【讨论】:

        【解决方案3】:

        尝试设置:android:adjustViewBounds=false 我可能弄错了,但是当它设置为true 时,如果没有图像,视图可能会缩放到 0 宽度和 0 高度。

        【讨论】:

          猜你喜欢
          • 2012-10-20
          • 1970-01-01
          • 2012-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-04
          • 2011-11-07
          • 1970-01-01
          相关资源
          最近更新 更多