简体   繁体   English

在Android中更改图片的背景色

[英]Change background color of image in Android

I found allot about changing the background color of an activity but not how to change the backgroundcolor of an imageview, like this as example: 我发现有关更改活动背景颜色的分配,而不是有关如何更改图像视图的背景颜色的分配,例如以下示例:

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:src="@drawable/ic_person_200x200"
            android:layout_margin="10dp"
            android:layout_span="3"
            android:background="#52D017"
            android:padding="1dp" />

Anyone? 任何人?

In your java code, after creating the ImageView object you can try calling the setBackgroundColor() method which is inherited from View. 在您的Java代码中,创建ImageView对象之后,您可以尝试调用从View继承的setBackgroundColor()方法。 Check out the docs for more info on this http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int) 在此http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)上查看文档以获取更多信息。

If adding android:background attribute to the <ImageView> doesn't work then you should apply it programmatically. 如果将android:background属性添加到<ImageView>不起作用,则应以编程方式应用它。

Remove android:background="#52D017" from the <ImageView> and add the following to your activity file <ImageView>删除android:background="#52D017"并将以下内容添加到您的活动文件中

ImageView backgroundImg = (ImageView) findViewById(R.id.imageView2);
backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM