天天看點

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值

以上!