官方不建议直接使用Dialog创建对话框,一般情况下使用子类AlertDialog代替使用,在项目实际开发中的话,建议使用FragmentDialog,方便生命周期的管理和复用。
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("这是标题")
.setMessage("这是具体的消息内容")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//确认按钮
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//消息按钮
}
})
.create();
dialog.show();
public class CustomDialog extends Dialog {
public CustomDialog(@NonNull Context context) {
super(context);
View layout = LayoutInflater.from(context)
.inflate(R.layout.dialog_layout, null);
setContentView(layout);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
250, getContext().getResources().getDisplayMetrics());
window.setAttributes(params);
}
}
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_layout);
dialog.show();
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
//设置了填充父控件,但是没有贴边的原因就是存在decorView的padding
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
//必须设置,否则没有效果
window.getDecorView().setBackgroundColor(Color.parseColor("#FFFFFF"));
设置动画文件
# 进入动画效果 window_enter.xml
# 移除动画效果 window_out.xml
设置动画属性
# 在value/style.xml 下定义 dialogWindowAnimator
将设置的动画属性加入到代码中进行设置
window.setWindowAnimations(R.style.dialogWindowAnimator);
传统的new AlertDialog在屏幕旋转时,第一不会保存用户输入的值,第二还会报异常,因为Activity销毁前不允许对话框未关闭。而通过DialogFragment实现的对话框则可以完全不必考虑旋转的问题。
使用DialogFragment创建dialog有两种,一种是使用onCreateView
按照创建布局的形式创建,另一种是使用onCreateDialog
使用AlertDialog或者Dialog创建。
public class CustomDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
window.getDecorView().setPadding(0, 0, 0, 0);
params.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
window.getDecorView().setBackgroundColor(Color.WHITE);
//隐藏状态栏
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return inflater.inflate(R.layout.dialog_layout, container);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//do somethings
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog dialog = new AlertDialog.Builder(getContext())
//~~~
.create();
return dialog;
}
setNeutralButton
setItems
传入一个字符串集合setSingleChoiceItems
setMultiChoiceItems
参考
1、https://blog.csdn.net/lmj623565791/article/details/37815413