Android中Dialog位置的设置

效果图如下:
在这里插入图片描述
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);

    }
}

拓展:Android中Dialog样式的设置

发布了77 篇原创文章 · 获赞 411 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/qq_42761395/article/details/100577583