Dialog源码解析

构造方法:

Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
    if (createContextThemeWrapper) {
        if (themeResId == 0) {
            final TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);
            themeResId = outValue.resourceId;
        }
        mContext = new ContextThemeWrapper(context, themeResId);
    } else {
        mContext = context;
    }

    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    final Window w = new PhoneWindow(mContext);
    mWindow = w;
    w.setCallback(this);
    w.setOnWindowDismissedCallback(this);
    w.setWindowManager(mWindowManager, null, null);
    w.setGravity(Gravity.CENTER);

    mListenersHandler = new ListenersHandler(this);
}
protected Dialog(@NonNull Context context, boolean cancelable,
        OnCancelListener cancelListener) {
    this(context);
    mCancelable = cancelable;
    setOnCancelListener(cancelListener);
}


创建dialog的时候,我们会加载我们选择的样式style,创建context 初始化WindowManager Window
同时Dialog实现Callback接口的各个方法,(主要是一些事件的处理)
为window设置windowManager 设置window的Gravity重心位置
另外实现了: DialogInterface KeyEvent. Callback
添加设置取消监听器
DialogInterface 就是为Dilaog特意创建的接口: KeyEvent.Callback就是事件的回调


public interface Callback {
public boolean dispatchKeyEvent(KeyEvent event);
public boolean dispatchKeyShortcutEvent(KeyEvent event);
public boolean dispatchTouchEvent(MotionEvent event);
public boolean dispatchTrackballEvent(MotionEvent event);
public boolean dispatchGenericMotionEvent(MotionEvent event);
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event);
@Nullable
public View onCreatePanelView(int featureId);
public boolean onMenuItemSelected(int featureId, MenuItem item);
public void onWindowAttributesChanged(WindowManager.LayoutParams attrs);
public void onContentChanged();
public void onWindowFocusChanged(boolean hasFocus);
public void onAttachedToWindow();
public void onDetachedFromWindow();
}
还实现了OnWindowDismissedCallback接口

/** @hide */
public interface OnWindowDismissedCallback {
    /**
     * Called when a window is dismissed. This informs the callback that the
     * window is gone, and it should finish itself.
     */
    public void onWindowDismissed();
}

调用show方法的时候:根据WindowManager.LayoutParams设置窗体的位置

WindowManager.LayoutParams l = mWindow.getAttributes();

常用api详解:

/**
 * Sets whether this dialog is cancelable with the
 * {@link KeyEvent#KEYCODE_BACK BACK} key.
 */
public void setCancelable(boolean flag) {
    mCancelable = flag;
}
/**
 * Sets whether this dialog is canceled when touched outside the window's
 * bounds. If setting to true, the dialog is set to be cancelable if not
 * already set.
 * 
 * @param cancel Whether the dialog should be canceled when touched outside
 *            the window.
 */
public void setCanceledOnTouchOutside(boolean cancel) {
    if (cancel && !mCancelable) {
        mCancelable = true;
    }
    
    mWindow.setCloseOnTouchOutside(cancel);
}


注意:其实dialog和activity一样都是将自己的decorview添加到了Window里面

window的其实可以被当作是一个父view 

Window window = dialog.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
params.y = (int) context.getResources().getDimension(R.dimen.x64);
window.setAttributes(params);

 通过window的Attributes属性 我们可以设置decorview的显示位置 大小的控制。




































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值