setBackgroundResource(0)

转载网址:http://blog.csdn.net/cb269267/article/details/10065709

今天发现了一个比较坑爹的bug,得出的结论如下,不要轻易相信Android的api。

当你调用setBackgroundResource(id)的时候实际上是调用的setBackgroundDrawable(d)

源码如下:

[java]  view plain  copy
 print ?
  1. /** 
  2.  * Set the background to a given resource. The resource should refer to 
  3.  * a Drawable object or 0 to remove the background. 
  4.  * @param resid The identifier of the resource. 
  5.  * @attr ref android.R.styleable#View_background 
  6.  */  
  7. @RemotableViewMethod  
  8. public void setBackgroundResource(int resid) {  
  9.     if (resid != 0 && resid == mBackgroundResource) {  
  10.         return;  
  11.     }  
  12.   
  13.     Drawable d= null;  
  14.     if (resid != 0) {  
  15.         d = mResources.getDrawable(resid);  
  16.     }  
  17.     setBackgroundDrawable(d);  
  18.   
  19.     <span style="color:#ff6666;">mBackgroundResource = resid;</span>  
  20. }  

然后在setBackgroundDrawable(d)的时候,实际上它是将backgroundDrawable的padding设置成了这个view的padding的。

源码如下:

[java]  view plain  copy
 print ?
  1. /** 
  2.      * Set the background to a given Drawable, or remove the background. If the 
  3.      * background has padding, this View's padding is set to the background's 
  4.      * padding. However, when a background is removed, this View's padding isn't 
  5.      * touched. If setting the padding is desired, please use 
  6.      * {@link #setPadding(int, int, int, int)}. 
  7.      * 
  8.      * @param d The Drawable to use as the background, or null to remove the 
  9.      *        background 
  10.      */  
  11.     public void setBackgroundDrawable(Drawable d) {  
  12.         if (d == mBGDrawable) {  
  13.             return;  
  14.         }  
  15.   
  16.         boolean requestLayout = false;  
  17.   
  18.         mBackgroundResource = 0;  
  19.   
  20.         /* 
  21.          * Regardless of whether we're setting a new background or not, we want 
  22.          * to clear the previous drawable. 
  23.          */  
  24.         if (mBGDrawable != null) {  
  25.             mBGDrawable.setCallback(null);  
  26.             unscheduleDrawable(mBGDrawable);  
  27.         }  
  28.   
  29.         if (d != null) {  
  30.             Rect padding = sThreadLocal.get();  
  31.             if (padding == null) {  
  32.                 padding = new Rect();  
  33.                 sThreadLocal.set(padding);  
  34.             }  
  35.             if (d.getPadding(padding)) {  
  36.                 switch (d.getResolvedLayoutDirectionSelf()) {  
  37.                     case LAYOUT_DIRECTION_RTL:  
  38.                         setPadding(padding.right, padding.top, padding.left, padding.bottom);  
  39.                         break;  
  40.                     case LAYOUT_DIRECTION_LTR:  
  41.                     default:  
  42.                         setPadding(padding.left, padding.top, padding.right, padding.bottom);  
  43.                 }  
  44.             }  
  45.   
  46.             // Compare the minimum sizes of the old Drawable and the new.  If there isn't an old or  
  47.             // if it has a different minimum size, we should layout again  
  48.             if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||  
  49.                     mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {  
  50.                 requestLayout = true;  
  51.             }  
  52.   
  53.             d.setCallback(this);  
  54.             if (d.isStateful()) {  
  55.                 d.setState(getDrawableState());  
  56.             }  
  57.             d.setVisible(getVisibility() == VISIBLE, false);  
  58.             mBGDrawable = d;  
  59.   
  60.             if ((mPrivateFlags & SKIP_DRAW) != 0) {  
  61.                 mPrivateFlags &= ~SKIP_DRAW;  
  62.                 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;  
  63.                 requestLayout = true;  
  64.             }  
  65.         } else {  
  66.             /* Remove the background */  
  67.             mBGDrawable = null;  
  68.   
  69.             if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {  
  70.                 /* 
  71.                  * This view ONLY drew the background before and we're removing 
  72.                  * the background, so now it won't draw anything 
  73.                  * (hence we SKIP_DRAW) 
  74.                  */  
  75.                 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;  
  76.                 mPrivateFlags |= SKIP_DRAW;  
  77.             }  
  78.   
  79.             /* 
  80.              * When the background is set, we try to apply its padding to this 
  81.              * View. When the background is removed, we don't touch this View's 
  82.              * padding. This is noted in the Javadocs. Hence, we don't need to 
  83.              * requestLayout(), the invalidate() below is sufficient. 
  84.              */  
  85.   
  86.             // The old background's minimum size could have affected this  
  87.             // View's layout, so let's requestLayout  
  88.             requestLayout = true;  
  89.         }  
  90.   
  91.         computeOpaqueFlags();  
  92.   
  93.         if (requestLayout) {  
  94.             requestLayout();  
  95.         }  
  96.   
  97.         mBackgroundSizeChanged = true;  
  98.         invalidate(true);  
  99.     }  

再回去看第一段源码的红色那行,你还标注了让人传个参数0就是去掉背景,TM的你就将mBackgroundResource设置成0就拍拍屁股走人了,之前你做过的操作你干嘛不还原呢?你默默地把padding改了,但是你不敢默默地把padding改回去,你偷懒写代码,连函数说明都懒得注明这情况


在此使劲拍你一砖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值