android ImageView中setBackground相关属性的区别

本文详细介绍了Android中ImageView背景设置的四种方法:setBackgroundColor、setBackgroundDrawable、setBackgroundResource及setBackground,并对比了它们之间的区别,帮助开发者更好地理解和使用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在写android程序时,遇到以下情形:

底部导航栏要用到4个ImageView来实现,在点击不同的ImageView是,要对其背景色进行设置,在使用ImageView关于setBackground相关属性设置时,会出现异常,从而导致android程序崩溃试了好多办法都不行,于是乎,就到android 官方网站查API,才把问题解决。写一篇博客记下来,防止以后再忘记。


首先看一下android官方给的文档:

public void setBackground (Drawable background)
Added in  API level 16

Set the background to a given Drawable, or remove the background. If the background has padding, this View's padding is set to the background's padding. However, when a background is removed, this View's padding isn't touched. If setting the padding is desired, please use setPadding(int, int, int, int).

Parameters
background The Drawable to use as the background, or null to remove the background

public void setBackgroundColor (int color)
Added in  API level 1

Sets the background color for this view.

Parameters
color the color of the background

public void setBackgroundDrawable (Drawable background)
Added in  API level 1

This method was deprecated in API level 16.
use setBackground(Drawable) instead


public void setBackgroundResource (int resid)
Added in  API level 1

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

Related XML Attributes
Parameters
resid The identifier of the resource.

从官方给出的API可以得出以下结论:

1.setBackgroundColor(),setBackgroundDrawable(),setBackgroundResource()三个方法从API 1开始就已经存在,而setBackgrouond()是从API 16才引进来的(Android 4.1开始),目的是为了取代setBackgroundDrawable().

所以如果开发的android API 在16+,则推荐使用setBackground().

2.三者区别:

setBackgroundColor(int color)

看其参数和说明可知,要传入一个颜色的ID值,可以使在colors.xml中定义的资源,也可以使用ARGB模式的十六进制数值来表示,如0xFFFF0000(红色);

setBackgroundResource(int resid)

这个方法在XML中有对应的属性android:background,可以理解为给ImageView设置背景,参数为资源的ID。

setBackground(Drawable background)

这个表示给ImageView设置背景,参数为资源,一般为图片,与其他不同的是,当背景有内边距的时候,那么View的内边距被设置和背景的内边距一样,当背景不存在的时候,View的背景也不存在。

### 如何在 Android 中使用 `background` 属性设置视图背景 #### 使用 XML 定义背景 可以在布局文件中通过 XML 来指定视图的背景。对于任何支持 `android:background` 的 View 组件,可以为其分配颜色、图片或形状作为背景。 ```xml <View android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/myColor" /> <!-- 颜色 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button with Image Background" android:background="@drawable/image_resource" /> <!-- 图片 --> ``` 为了给 LinearLayout 添加不拉伸的背景图像并保持原尺寸,在 XML 布局里应这样配置[^4]: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"> <ImageView android:id="@+id/backgroundImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image_here"/> </LinearLayout> ``` #### 动态设置背景 除了静态地在 XML 文件内设定外,还可以编程方式动态改变视图的背景: ```java // Java code to set background dynamically at runtime. view.setBackgroundResource(R.drawable.image_resource); ``` 当需要更复杂的操作比如动画化背景变化时,则可能要用到 `ViewCompat.setBackground()` 方法来实现平滑过渡效果[^2]. #### 处理重复背景问题 如果遇到因重复利用 drawable 对象而导致的背景显示异常情况,建议每次创建新的 Drawable 实例而不是重用旧的对象实例[^3]. 例如: ```java Drawable background = ContextCompat.getDrawable(context, R.drawable.selector_default_bg); myCustomView.setBackground(background); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值