2012-02-16 33 views
15

是否有Vs的透明(#00000000)沒有任何差異@null Vs的#00000000

在我的佈局@null任何區別我設置android:background"@color/transparent" 但它表明我使用了一些其他不同的背景顏色。

當我用空它的工作正常。

我想設置@null thru programmatic。

該怎麼辦?

+0

可能是錯在你color.xml或嘗試'@android:彩色/ transparent'。 – MKJParekh 2012-02-16 13:23:10

+0

@Soni與「00000000」和android:color有什麼不同。兩者都是一樣的權利。 – 2012-02-16 13:51:19

+0

感謝所有我只是困惑與setBackgroundColor()。 – 2012-02-16 14:38:17

回答

12

@null意味着在所有沒有背景(View.getBackground()返回null)

#00000000意味着您將ColorDrawable作爲具有完全透明顏色的背景。

我沒有看代碼,但我猜測框架測試ColorDrawable是否完全透明,並且在這種情況下不繪製它。否則,你會有一些繪圖開銷,使@null更快的選擇。兩者應該看起來完全相同,所以不確定這是否是你的基礎。

要在代碼中設置等效@null,請使用View.setBackgroundDrawable(null)

2

在背景中設置0。

view.setBackgroundColor(0); 
8

是的,有。

  • @null意味着沒有背景。
  • #00000000表示添加透明背景。

如果你不會有背景使它@null它應該表現更好。要使用@null從代碼,你可以嘗試做:

widget.setBackgroundDrawable(null); 
1

我會說,在大多數情況下比@android更喜歡@null背景:color/transparent。

在代碼中,使用setBackground(null)來調用不推薦使用的方法setBackgroundDrawable();

如果你看看View.setBackgroundDrawable(),你會注意到,如果你傳遞null作爲背景,它會將標誌設置爲SKIP_DRAW,就是這樣。另一方面,如果有可繪製的對象,它將通過額外的過程來設置背景填充。

這裏是setBackgroundDrawable的代碼(注:使用的setBackground代替setBackgroundDrawable)

public void setBackgroundDrawable(Drawable background) { 
    computeOpaqueFlags(); 

    if (background == mBackground) { 
     return; 
    } 

    boolean requestLayout = false; 

    mBackgroundResource = 0; 

    /* 
    * Regardless of whether we're setting a new background or not, we want 
    * to clear the previous drawable. 
    */ 
    if (mBackground != null) { 
     mBackground.setCallback(null); 
     unscheduleDrawable(mBackground); 
    } 

    if (background != null) { 
     Rect padding = sThreadLocal.get(); 
     if (padding == null) { 
      padding = new Rect(); 
      sThreadLocal.set(padding); 
     } 
     resetResolvedDrawables(); 
     background.setLayoutDirection(getLayoutDirection()); 
     if (background.getPadding(padding)) { 
      resetResolvedPadding(); 
      switch (background.getLayoutDirection()) { 
       case LAYOUT_DIRECTION_RTL: 
        mUserPaddingLeftInitial = padding.right; 
        mUserPaddingRightInitial = padding.left; 
        internalSetPadding(padding.right, padding.top, padding.left, padding.bottom); 
        break; 
       case LAYOUT_DIRECTION_LTR: 
       default: 
        mUserPaddingLeftInitial = padding.left; 
        mUserPaddingRightInitial = padding.right; 
        internalSetPadding(padding.left, padding.top, padding.right, padding.bottom); 
      } 
      mLeftPaddingDefined = false; 
      mRightPaddingDefined = false; 
     } 

     // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or 
     // if it has a different minimum size, we should layout again 
     if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() || 
       mBackground.getMinimumWidth() != background.getMinimumWidth()) { 
      requestLayout = true; 
     } 

     background.setCallback(this); 
     if (background.isStateful()) { 
      background.setState(getDrawableState()); 
     } 
     background.setVisible(getVisibility() == VISIBLE, false); 
     mBackground = background; 

     if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) { 
      mPrivateFlags &= ~PFLAG_SKIP_DRAW; 
      mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND; 
      requestLayout = true; 
     } 
    } else { 
     /* Remove the background */ 
     mBackground = null; 

     if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) { 
      /* 
      * This view ONLY drew the background before and we're removing 
      * the background, so now it won't draw anything 
      * (hence we SKIP_DRAW) 
      */ 
      mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND; 
      mPrivateFlags |= PFLAG_SKIP_DRAW; 
     } 

     /* 
     * When the background is set, we try to apply its padding to this 
     * View. When the background is removed, we don't touch this View's 
     * padding. This is noted in the Javadocs. Hence, we don't need to 
     * requestLayout(), the invalidate() below is sufficient. 
     */ 

     // The old background's minimum size could have affected this 
     // View's layout, so let's requestLayout 
     requestLayout = true; 
    } 

    computeOpaqueFlags(); 

    if (requestLayout) { 
     requestLayout(); 
    } 

    mBackgroundSizeChanged = true; 
    invalidate(true); 
}