自定义dialog

自定义dialog_第1张图片

public class MyDialog   extends   Dialog {

Context context;

public MyDialog(Context context) {

super(context);

this.context= context;

}

public MyDialog(Context context, int themeResId) {

super(context,themeResId);

this.context= context;

}

public void stopDialog() {

dismiss();

}

public void startDialog() {

View inflate = LayoutInflater.from(context).inflate(R.layout.activity_dialog, null);

Button upgrade = (Button) inflate.findViewById(R.id.upgrade);

ImageView cancel = (ImageView) inflate.findViewById(R.id.cancel)

//获取手机屏幕的宽高

WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);

Display display = wm.getDefaultDisplay();

ViewGroup.LayoutParamslayoutParams=newViewGroup.LayoutParams((int) (display.getWidth() *0.8),(int) (display.getHeight() *0.7));

//设置自定义布局(参数一:设置View,参数二:设置dialog的大小)

setContentView(inflate,layoutParams);

//true==>允许点击dialog外位置关闭dialog,false==>不允许点击dialog外位置关闭dialog

setCancelable(false);

//设置展示

show();

upgrade.setOnClickListener(newView.OnClickListener() {

@Override

public void onClick(View view) {

stopDialog();

}

});

cancel.setOnClickListener(newView.OnClickListener() {

@Override

public void onClick(View view) {

stopDialog();

}

});

}

}

你可能感兴趣的:(自定义dialog)