有没有差异@null Vs#00000000

时间:2012-02-16 12:17:25

标签: android

@null Vs transparent(#00000000)

之间有什么区别吗?

在我的布局中,我设置了android:background"@color/transparent" 但它显示了我使用的其他不同的背景颜色。

当我使用null时工作正常。

我想设置@null通过programmatic。

怎么办?

4 个答案:

答案 0 :(得分:13)

@null表示根本没有背景(View.getBackground()返回null)

#00000000表示您将ColorDrawable作为背景,具有完全透明的颜色。

我没有查看代码,但我想框架测试ColorDrawable是否完全透明,并且在这种情况下不会绘制它。否则你会有一些绘图开销,使@null更快。两者看起来应该相同,所以不确定这是否是你的基础。

要在代码中设置等效的@null,请使用View.setBackgroundDrawable(null)

答案 1 :(得分:8)

是的,有。

  • @null表示没有背景。
  • #00000000表示添加透明背景。

如果你没有背景,请@null它应该表现得更好。要使用代码中的@null,您可以尝试:

widget.setBackgroundDrawable(null);

答案 2 :(得分:2)

在后台设置为0。

view.setBackgroundColor(0);

答案 3 :(得分:1)

我想说,在大多数情况下,更喜欢@null背景而不是@android: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);
}