最近發(fā)現(xiàn),自定義的Dialog,顯示的內(nèi)容外有一定大小的外間距(有點(diǎn)類似margin的效果)和Dialog里面的Window對象是否setBackgroundDrawable(Drawable drawable)有關(guān),親測是這樣的。現(xiàn)象直接請看圖。
1.調(diào)用Window的setBackgroundDrawable

WX20191031-1.png
2.沒有調(diào)用Window的setBackgroundDrawable

WX20191031-2.png
查看了下源碼,貌似只有在DecorView.java中有setWindowBackground后關(guān)聯(lián)了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進(jìn)行設(shè)置
private void drawableChanged() {
if (mChanging) {
return;
}
setPadding(mFramePadding.left + mBackgroundPadding.left,
mFramePadding.top + mBackgroundPadding.top,
mFramePadding.right + mBackgroundPadding.right,
mFramePadding.bottom + mBackgroundPadding.bottom);
// 省略其它代碼
.......
}
至此還是沒找到這個間距來龍去脈,系統(tǒng)是怎么設(shè)置的,具體值對應(yīng)多少,望知曉的大神不吝賜教。??
另外還有一個問題就是,自定義的Dialog的構(gòu)造方法回傳Context給父類Dialog時,沒有調(diào)用Window的setBackgroundDrawable,如果直接傳Context,顯示內(nèi)容會有黑邊框,然而傳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);
}
......
}