2012-02-16 27 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); 
}