自定义Dialog所遇到的问题总结

自定义Dialog所遇到的问题总结

1. 在extents Dialog 后,对话框左右两边出现大概10dp的margin,通过自定义其style无效后的解决办法

解决方法:

   Window window = dialog.getWindow();
           WindowManager.LayoutParams params =
                   window.getAttributes();
           params.width =  WindowManager.LayoutParams.MATCH_PARENT;
           params.height = WindowManager.LayoutParams.WRAP_CONTENT;
           params.gravity =Gravity.FILL_HORIZONTAL;
           window.setAttributes(params);

说明:主要代码为

           params.gravity =Gravity.FILL_HORIZONTAL;

2. extends Dialog 与 extends AlertDialog 引发的问题:前者可以调取软键盘,后者不起作用,这里写一下分析过程,问题虽然还没找到原因。

注意此问题都是通过自定义其style实现。

过程:

1.从extentds AlertDialog 开始向下追踪

public class CustomBottomDialog extends AlertDialog {
   public CustomBottomDialog(Context context, int themeResId) {
       super(context, themeResId);
   }
}
  protected AlertDialog(Context context, @StyleRes int themeResId) {
       this(context, themeResId, true);
   }

   AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
       super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
               createContextThemeWrapper);

       mWindow.alwaysReadCloseOnTouchAttr();
       mAlert = AlertController.create(getContext(), this, getWindow());
   }
}
static @StyleRes int resolveDialogTheme(Context context, @StyleRes int themeResId) {
       if (themeResId == THEME_TRADITIONAL) {
           return R.style.Theme_Dialog_Alert;
       } else if (themeResId == THEME_HOLO_DARK) {
           return R.style.Theme_Holo_Dialog_Alert;
       } else if (themeResId == THEME_HOLO_LIGHT) {
           return R.style.Theme_Holo_Light_Dialog_Alert;
       } else if (themeResId == THEME_DEVICE_DEFAULT_DARK) {
           return R.style.Theme_DeviceDefault_Dialog_Alert;
       } else if (themeResId == THEME_DEVICE_DEFAULT_LIGHT) {
           return R.style.Theme_DeviceDefault_Light_Dialog_Alert;
       } else if (ResourceId.isValid(themeResId)) {
           // start of real resource IDs.
           return themeResId;
       } else {
           final TypedValue outValue = new TypedValue();
           context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
           return outValue.resourceId;
       }
   }
Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
       if (createContextThemeWrapper) {
           if (themeResId == ResourceId.ID_NULL) {
               final TypedValue outValue = new TypedValue();
               context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);
               themeResId = outValue.resourceId;
           }
           mContext = new ContextThemeWrapper(context, themeResId);
       } else {
           mContext = context;
       }
      }

如果extends Dialog 直接到最有一步,本质上都一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值