Android自定義Dialog之疑問請教

最近發(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);
    }

   ......
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容