Android自定义Dialog之疑问请教

最近发现,自定义的Dialog,显示的内容外有一定大小的外间距(有点类似margin的效果)和Dialog里面的Window对象是否setBackgroundDrawable(Drawable drawable)有关,亲测是这样的。现象直接请看图。
1.调用Window的setBackgroundDrawable


WX20191031-1.png

2.没有调用Window的setBackgroundDrawable


WX20191031-2.png

查看了下源码,貌似只有在DecorView.java中有setWindowBackground后关联了mBackgroundPadding的东西

    public void setWindowBackground(Drawable drawable) {
        if (getBackground() != drawable) {
            setBackgroundDrawable(drawable);
            if (drawable != null) {
                mResizingBackgroundDrawable = enforceNonTranslucentBackground(drawable,
                        mWindow.isTranslucent() || mWindow.isShowingWallpaper());
            } else {
                mResizingBackgroundDrawable = getResizingBackgroundDrawable(
                        getContext(), 0, mWindow.mBackgroundFallbackResource,
                        mWindow.isTranslucent() || mWindow.isShowingWallpaper());
            }
            if (mResizingBackgroundDrawable != null) {
                mResizingBackgroundDrawable.getPadding(mBackgroundPadding);
            } else {
                mBackgroundPadding.setEmpty();
            }
            drawableChanged();
        }
    }

然后再是drawableChanged中有对Padding进行设置

    private void drawableChanged() {
        if (mChanging) {
            return;
        }

        setPadding(mFramePadding.left + mBackgroundPadding.left,
                mFramePadding.top + mBackgroundPadding.top,
                mFramePadding.right + mBackgroundPadding.right,
                mFramePadding.bottom + mBackgroundPadding.bottom);
       // 省略其它代码
       .......
    }

至此还是没找到这个间距来龙去脉,系统是怎么设置的,具体值对应多少,望知晓的大神不吝赐教。

另外还有一个问题就是,自定义的Dialog的构造方法回传Context给父类Dialog时,没有调用Window的setBackgroundDrawable,如果直接传Context,显示内容会有黑边框,然而传ContextThemeWrapper的时候就没有,如下图:


WX20191031-3.png
public class PermissionDialog extends Dialog {

    protected PermissionDialog(Context context) {
        this(context, 0);
    }

    protected PermissionDialog(Context context, int theme) {
        super(new ContextThemeWrapper(context, theme), theme);
    }

   ......
}

你可能感兴趣的:(Android自定义Dialog之疑问请教)