getWindow()。setBackgroundDrawable和getWindow()之间的区别.getDecorView()。setBackgroundDrawable

时间:2016-07-21 09:02:05

标签: android android-layout android-view android-windowmanager

他们之间有什么区别?据我所见,他们都做同样的事情。

1 个答案:

答案 0 :(得分:1)

如果您访问来源,您会发现PhoneWindow.class已实施Window.setBackgroundDrawable方法:

@Override
public final void setBackgroundDrawable(Drawable drawable) {
    if (drawable != mBackgroundDrawable || mBackgroundResource != 0) {
        mBackgroundResource = 0;
        mBackgroundDrawable = drawable;
        if (mDecor != null) {
            mDecor.setWindowBackground(drawable);
        }
        if (mBackgroundFallbackResource != 0) {
            mDecor.setBackgroundFallback(drawable != null ? 0 : mBackgroundFallbackResource);
        }
    }
}

如您所见,它调用了DecorView setWindowBackground的不同方法:

public void setWindowBackground(Drawable drawable) {
    if (getBackground() != drawable) {
        setBackgroundDrawable(drawable);
        if (drawable != null) {
            drawable.getPadding(mBackgroundPadding);
        } else {
            mBackgroundPadding.setEmpty();
        }
        drawableChanged();
    }
}

实际上使用方法DecorView.setBackgroundDrawable,但也为Window提供了额外的逻辑。所以我建议你使用getWindow().setBackgroundDrawable()