在android

时间:2018-05-16 14:19:09

标签: android

我有ImageViews来显示客户图片。如果客户的图像不可用,我想在ImageView中显示灰色背景。

编辑:这是来自'de.hdodenhof:Circleimageview:2.2.0'的CircleImageview 当我设置backgroundColor时,它变成一个矩形而不是圆形。

6 个答案:

答案 0 :(得分:2)

使用background

ImageView属性

由于背景需要为圆形,因此使用形状xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid 
       android:color="#A9A9A9"/> <!-- you can set the color you want here -->

    <!-- you can also define size if required-->

</shape>

然后将其用作背景

android:background="@drawable/your_file_id"

这样当显示图像时它会隐藏背景颜色..如果你没有设置图像,背景就会显示

更新
为了避免在设置图像资源时出现部分透明图像和比率降低背景的问题

答案 1 :(得分:1)

使用:

android:tint="@color/colorAccent"

答案 2 :(得分:1)

如果您需要动态地执行此操作:

清除以前的所有图片资源:

img.setImageResource(0);

然后

img.setBackgroundColor(Color.rgb(206, 206, 204));

img.setBackgroundResource(R.color.someGrayColor)

有更多选项可以为视图设置背景颜色。您可以在线查看。

答案 3 :(得分:1)

如果您使用的是CircularImageView,则可以创建一个圆形drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#808080"
        />

    <size
        android:height="120dp"
        android:width="120dp"
        />
</shape>

然后,只要客户的图像不可用,就将其设置为资源。

答案 4 :(得分:0)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#808080"/>

<size android:height="25dp"android:width="25dp"/>
</shape>

当图像不可用时,请将其用作资源。

答案 5 :(得分:0)

您可以通过在XML中添加此代码来设置任何布局,任何视图或任何组件的背景透明:

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