概述
Dialog 改宽度的需求还是比较多的,所以继上一篇 图片凸出 文章的代码继续写写自定度 Dialog 的宽度,官方推荐用 DialogFragment ,原因网上有的此处不说了。
代码
自定义宽度与layout三大布局一样,都是拿到 LayoutParams 来设置各种属性,但是AlertDialog怎么弄也不行,没有研究透失败失败,此处直接用的new Dialog,下面看代码
public class CheckoutMoneyDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_checkout_money_dialog, null);
builder.setView(view);
Dialog dialog = builder.create();
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
}
再来看看有种仿IOS的需求,要让Dialog在屏幕下方,并且是满屏,
public class BottomFullWidthDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.fragment_checkout_money_dialog);
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.gravity = Gravity.BOTTOM;//下方
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;//满屏
window.setAttributes(attributes);
return dialog;
}
}
欢迎关注公众号,每天为您推送“人生感悟、说话技巧、职场规则、职场成长”