效果图如下:
Dialog实现如下:
在Activity的点击事件中调用:
MyDialog.java:
public class MyDialog extends Dialog {
public MyDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//打气
setContentView(R.layout.mydialog);
//窗口
Window window = getWindow();
//拿到窗口参数对象
WindowManager.LayoutParams attributes = window.getAttributes();
//通过attributes设置参数,----->以设置dialog出现在底部为例
attributes.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
//将属性重新设置给窗口,使用更新属性来生效
window.setAttributes(attributes);
}
}