天天看点

setBackground,setBackgroundColor,setBackgroundDrawable,setBackgroundResource的使用区别

上面四个方法,在代码中设置其背景颜色,有时候就会傻傻的分不清楚!

第一个:setBackground(Drawable background),其参数为一个Drawable对象,目的是设置View的背景图片,获取drawable可以这样获取–getResources().getDrawable(R.drawable.xx)

第二个:setBackgroundColor(int color),其参数是一个颜色值,设置View的背景颜色.其color可以写为Color.parseColor(“#333333”)或者Color.Blue

第三个:setBackgroundDrawable,和第一个是一样的,现在已经过时淘汰了,不建议使用

第四个:setBackgroundResource(int resid)是设置一个view的背景图片,只不过传入的是drawable的id值

以上!