Android 是否存在任何差异@null与#00000000

Android 是否存在任何差异@null与#00000000,android,Android,@null和透明(#00000000)之间有什么区别吗 在我的布局中,我设置了android:background“@color/transparent” 但它显示了我使用的其他一些不同的背景色 当我使用null时,它工作得很好 我想通过编程设置@null 如何操作?在后台设置0 view.setBackgroundColor(0); 是的,有 @null表示没有背景 #00000000表示添加透明背景 如果您没有背景,请将其设置为@null,它的性能会更好。要使用代码中的@null,您可

@null和透明(#00000000)之间有什么区别吗

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

当我使用null时,它工作得很好

我想通过编程设置@null

如何操作?

在后台设置0

view.setBackgroundColor(0);
是的,有

  • @null
    表示没有背景
  • #00000000
    表示添加透明背景
如果您没有背景,请将其设置为
@null
,它的性能会更好。要使用代码中的
@null
,您可以尝试执行以下操作:

widget.setBackgroundDrawable(null);

@null
表示完全没有背景(View.getBackground()返回null)

#00000000
意味着您有一个可着色的背景,具有完全透明的颜色

我没有查看代码,但我想框架会测试ColorDrawable是否完全透明,并且在本例中不会绘制它。否则会有一些绘图开销,使
@null
成为更快的选择。两者看起来应该相同,所以不确定这是否是您的基本isse


要在代码中设置与
@null
等效的值,请使用。

我想说的是,在大多数情况下,更喜欢@null background而不是@android:color/transparent

在代码中,使用setBackground(null),它调用不推荐使用的方法setBackgroundDrawable()

如果查看View.setBackgroundDrawable(),您会注意到,如果将null作为背景传递,它会将标志设置为SKIP_DRAW,仅此而已。另一方面,若有一个可绘制的对象,它将经历额外的过程来设置背景填充

这是setBackgroundDrawable的代码(注:使用setBackground而不是setBackgroundDrawable)


您的color.xml中可能有错误,或者尝试
@android:color/transparent
@Soni是否与“00000000”和android:color有任何区别。两个都是一样的。谢谢你刚才把我和setBackgroundColor()搞混了。
   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);
}