android创建popupwindow和AlertDialog

创建popupwindow


//为popupwindow创建指定的布局,inflate内分别填写上下文,layout资源等
View Contentview = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_layout,null,false);
//方法二:View Contentview = View.inflate(MainActivity.this,R.layout.pop_layout,null);
//创建popupwindow,分别放入所需的视图,xy坐标,是否能够焦点
PopupWindow popupWindow = new PopupWindow(Contentview,400,400);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.BLUE));

//展示popupwindow
popupWindow.showAsDropDown(Contentview);

效果如下图
android创建popupwindow和AlertDialog


创建AlertDialog


AlertDialog.Builder dialog1 = new AlertDialog.Builder(MainActivity.this);
dialog1.setTitle("标题");
dialog1.setMessage("内容");
dialog1.setCancelable(false);
dialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});

dialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});
dialog1.show();

效果如下图

android创建popupwindow和AlertDialog