Android之dialog实现底部出现对话框

来源:互联网 发布:java array list 编辑:程序博客网 时间:2024/05/24 01:37

首先 是自己定义的MyDialog

public class MyDialog extends Dialog {    Activity context;    View view;    /*对话框宽度    * 宽度适应中间对话框宽度 为屏幕1/3    * 为2 适应底部对话框 宽度为屏幕宽度    */    int gravityMode;    public MyDialog(Activity context,View view,int gravityMode) {        super(context);        this.context = context;        this.view = view;        this.gravityMode = gravityMode;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        getWindow().setBackgroundDrawableResource(R.color.bantouming_color);        /*view = View.inflate(this.context, R.layout.dialog_user_headimg, null);*/        setContentView(view);        initDialogSize();    }    private void initDialogSize(){        WindowManager m = context.getWindowManager();        Display d = m.getDefaultDisplay();        WindowManager.LayoutParams p = getWindow().getAttributes();  //获取对话框当前的参数值        if (gravityMode == 1){            p.height = (int) (d.getHeight() * 0.25);   //高度设置为屏幕的0.35            p.width = (int) (d.getWidth() * 0.7);    //宽度设置为屏幕的0.7        }else if (gravityMode == 2){            p.height = (int) (d.getHeight() * 0.3);   //高度设置为屏幕的0.35            p.width = (int) (d.getWidth() * 0.9);    //宽度设置为屏幕的0.7        }        getWindow().setAttributes(p);     //设置生效    }}

然后就是在相应的地方,运用

若需求是在屏幕中间弹出对话框,则直接调用即可

MyDialog mDialog;

View view  = View.inflate(this,R.layout.***,null);

mDialog = new MyDialog(this,view,1);

mDialog.show();


如果需求是在底部弹出对话框,例如设置头像时候,在底部弹出对话框,让用户选择,相机拍照、相册选择或取消 

这时候只需像上面调用MyDialog之后,将

mDialog  = new MyDialog(thgis,view,2); 

并添加代码一句:

Window mWindow = mDialog.getWindow();mWindow.setGravity(Gravity.BOTTOM | Gravity.BOTTOM);

 即可!

0 0
原创粉丝点击