2012-02-16 6 views
15

対任意の差分@nullは、私がandroid:background"@color/transparent" が、その私が使用されるいくつかの他の異なる背景色を示すを設定し、私のレイアウトで透明(#00000000)#00000000

対@nullの間に違いがそこにあるあります。

私はnullを使用して正常に動作しました。

プログラムで@nullを設定したいと思います。

どのようにすればよいですか?

+0

があなたのcolor.xmlに間違いであるか試してみてください '@android:カラー/ transparent'。 – MKJParekh

+0

@ソニは、 "00000000"とアンドロイド:色との違いはありますか?どちらも同じです。 –

+0

私はちょうどsetBackgroundColor()と混乱しています。 –

答えて

12

@nullはすべてでバックグラウンドがないことを意味しを:コードから@nullを使用するには、やってみることができます。

#00000000は、完全に透明な色を持つ背景としてColorDrawableを取得したことを意味します。

私はコードを見ませんでしたが、フレームワークはColorDrawableが完全に透明で、この場合描画しないかどうかをテストしていると思います。そうしないと、描画オーバーヘッドが発生し、@nullがより速く選択されます。どちらも同じように見えるはずですので、これが基本的なものであるかどうかは分かりません。

@nullに相当するコードをコードに設定する場合は、View.setBackgroundDrawable(null)を使用してください。

2

をバックグラウンドで設定します。

view.setBackgroundColor(0); 
8

はい、あります。

  • @nullは背景を意味しない。
  • #00000000は、透明な背景を追加することを意味します。

バックグラウンドを持たない場合は、@nullの方がパフォーマンスが向上するはずです。 ()View.getBackground(nullを返します)

widget.setBackgroundDrawable(null); 
1

私は、ほとんどの場合、@ androidより@nullの背景が好きだと言います:color/transparent。

コードでは、廃止予定のメソッドsetBackgroundDrawable()を呼び出すsetBackground(null)を使用します。

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); 
} 
関連する問題