2012-02-16 7 views
15

alguna diferencia entre @null Vs transparente (# 00000000)no existe ninguna @null diff Vs # 00000000

en mi diseño puse android:background"@color/transparent" pero su mostrando algún otro color de fondo diferente que he usado.

cuando usé null funciona bien.

Quiero establecer @null thru programmatic.

cómo hacerlo?

+0

puede haber error en su color.xml o tratar de '@android: Color/transparent'. – MKJParekh

+0

@Soni ¿hay alguna diferencia de "00000000" y de android: color. ambos tienen el mismo derecho. –

+0

gracias por todo lo que acabo de confundir con setBackgroundColor(). –

Respuesta

12

@null significa que no hay fondo en absoluto (View.getBackground() devuelve un valor nulo).

#00000000 significa que tiene un ColorDrawable como fondo que tiene un color completamente transparente.

No miré el código, pero supongo que el framework prueba si el ColorDrawable es completamente transparente y no lo dibuja en este caso. De lo contrario, tendría un poco de sobrecarga de dibujo, por lo que @null es la opción más rápida. Ambos deben ser idénticos, por lo que no estoy seguro de si este es tu problema subyacente.

Para establecer el equivalente a @null en el código, use View.setBackgroundDrawable(null).

2

conjunto 0 en el fondo.

view.setBackgroundColor(0); 
8

Sí, la hay.

  • @null significa que no hay fondo.
  • #00000000 significa agregar un fondo transparente.

Si no tienes un fondo hazlo @null debería funcionar mejor. Para utilizar @null del código que puede intentar hacer:

widget.setBackgroundDrawable(null); 
1

Yo diría que, en la mayoría de las circunstancias, prefiero @null background sobre @android: color/transparent.

En el código, use setBackground (null) que invoca un método obsoleto setBackgroundDrawable();

Si mira View.setBackgroundDrawable(), notará que si pasa nulo como fondo, establecerá las banderas en SKIP_DRAW y eso es todo. Por otro lado, si hay un objeto dibujable, pasará por un proceso adicional para configurar el relleno de fondo.

Aquí está el código de setBackgroundDrawable (Nota: el uso setBackground en lugar de 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); 
} 
Cuestiones relacionadas