2012-02-16 25 views
15

Có bất kỳ sự khác biệt giữa @null Vs minh bạch (# 00000000)có bất kỳ @null diff Vs # 00000000

trong cách bố trí của tôi, tôi thiết lập android:background"@color/transparent" nhưng thấy nó một số màu nền khác nhau khác mà tôi sử dụng.

khi tôi sử dụng không có tiền phạt hoạt động.

Tôi muốn đặt @null thru có lập trình.

cách thực hiện?

+0

có thể là sai lầm trong color.xml của bạn hoặc thử '@android: màu/transparent'. – MKJParekh

+0

@Soni là có bất kỳ khác biệt từ "00000000" và từ android: màu sắc. cả hai đều giống nhau. –

+0

cảm ơn tất cả những gì tôi vừa nhầm lẫn với setBackgroundColor(). –

Trả lời

12

@null nghĩa là không nền ở tất cả (View.getBackground() trả về null).

#00000000 có nghĩa là bạn có ColorDrawable làm nền có màu hoàn toàn trong suốt.

Tôi không xem mã, nhưng tôi đoán rằng các thử nghiệm khuôn khổ nếu ColorDrawable là hoàn toàn minh bạch và không vẽ nó trong trường hợp này. Nếu không, bạn sẽ có một số chi phí vẽ, làm cho @null là lựa chọn nhanh hơn. Cả hai sẽ trông giống hệt nhau, vì vậy không chắc chắn nếu điều này là isse cơ bản của bạn.

Để đặt số tương đương @null bằng mã, hãy sử dụng View.setBackgroundDrawable(null).

2

đặt 0 ở chế độ nền.

view.setBackgroundColor(0); 
8

Có, có.

  • @null nghĩa là không có nền.
  • #00000000 có nghĩa là thêm nền trong suốt.

Nếu bạn không có nền làm cho nó @null, nó sẽ hoạt động tốt hơn. Để sử dụng @null từ mã bạn có thể thử thực hiện:

widget.setBackgroundDrawable(null); 
1

Tôi sẽ nói, trong hầu hết các trường hợp, thích @null background trên @android: color/transparent.

Trong mã, hãy sử dụng setBackground (null) gọi phương thức không được chấp nhậnBackBackDrawable();

Nếu bạn nhìn vào View.setBackgroundDrawable(), bạn sẽ nhận thấy rằng nếu bạn vượt qua null làm nền, nó sẽ đặt cờ thành SKIP_DRAW và đó là nó. Mặt khác, nếu có một đối tượng có thể kéo được, nó sẽ trải qua quá trình bổ sung để thiết lập lớp đệm nền.

Đây là mã của setBackgroundDrawable (Lưu ý: sử dụng setBackground thay vì 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); 
} 
Các vấn đề liên quan