用LayoutInflater 定制 Dialog显示内容 并 调整Dialog的显示位置

时间:2022-06-06 08:27:31

AlertDialog alertDialog;
AlertDialog.Builder builder;
//实例化 LayoutInflater
LayoutInflater inflater=(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
//main1为对话框布局
View Layout=inflater.inflate(R.layout.main1, null);
TextView text=(TextView)Layout.findViewById(R.id.viewpaper1);
ImageView iamge=(ImageView)Layout.findViewById(R.id.viewpaper);
iamge.setImageResource(R.drawable.ic_launcher);
builder=new AlertDialog.Builder(this);

alertDialog=builder.create();
alertDialog.setView(Layout);
// 调整对话框的位置
Window mWindow=alertDialog.getWindow();
WindowManager.LayoutParams lp=mWindow.getAttributes();
lp.x=0;
lp.y=50;
alertDialog.onWindowAttributesChanged(lp);
alertDialog.show();