Drawable drawable = null;设置.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.masking_head));
时间: 2024-04-22 10:26:13
浏览: 31
在最新的Android[版本](https://geek.csdn.net/educolumn/04449e2eb7da35a9c72dce97dbf43e2a?spm=1055.2569.3001.10083)中,`setBackgroundDrawable()`方法已经被废弃了,推荐使用`setBackground()`方法来设置背景。
要设置一个drawable作为背景,可以使用以下代码:
```java
Drawable drawable = mContext.getResources().getDrawable(R.mipmap.masking_head);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
这段代码首先获取指定的drawable资源,然后根据当前设备的Android[版本](https://geek.csdn.net/educolumn/04449e2eb7da35a9c72dce97dbf43e2a?spm=1055.2569.3001.10083),使用不同的方法来设置背景。如果设备的Android[版本](https://geek.csdn.net/educolumn/04449e2eb7da35a9c72dce97dbf43e2a?spm=1055.2569.3001.10083)高于等于JELLY_BEAN(API级别16),则调用`setBackground()`方法;否则,调用`setBackgroundDrawable()`方法来设置背景。
注意:在使用`setBackgroundDrawable()`方法时,需要确保你的最低支持的Android[版本](https://geek.csdn.net/educolumn/04449e2eb7da35a9c72dce97dbf43e2a?spm=1055.2569.3001.10083)是JELLY_BEAN或更高。如果你的最低支持[版本](https://geek.csdn.net/educolumn/04449e2eb7da35a9c72dce97dbf43e2a?spm=1055.2569.3001.10083)较低,可以根据需要进行适当调整。
相关问题
下面资源调用写法有误吗 getResources().getDrawable(R.drawable.icon);
从Android 5.0(API级别21)开始,getDrawable(int)方法已被弃用,建议使用 ContextCompat.getDrawable(Context, int) 方法替代。因此,如果你的应用程序的 minSdkVersion 为 21 或更高版本,则应该使用以下代码来获取 drawable 资源:
```java
ContextCompat.getDrawable(context, R.drawable.icon);
```
int[] imageA={R.drawable.y01,R.drawable.y02,R.drawable.y03,R.drawable.y04, R.drawable.y05};
相关推荐














