public class ActionRuleDialog extends Dialog {
private static int default_width = 300; // 默认宽度
private static int default_height = 350;// 默认高度
public ActionRuleDialog(Context context, View layout, int style) {
this(context, default_width, default_height, layout, style);
}
public ActionRuleDialog(Context context, int width, int height, View layout,
int style) {
super(context, style);
setContentView(layout);
float density = getDensity(context);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = (int) (width * density);
params.height = (int) (height * density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
private float getDensity(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.density;
}
}
MainActivity:
<pre name="code" class="java"><span style="white-space:pre"> </span>final ActionRuleDialog dia;
LayoutInflater inflater2 = LayoutInflater.from(this);
View merge_view = inflater2.inflate(R.layout.dialog_countdown_actionrule,null);
dia = new ActionRuleDialog(this, merge_view,R.style.dialog); dia.show();
ImageView cancle = (ImageView) dia.findViewById(R.id.iv_coutdown_diacancel);
cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dia.cancel();// 关闭弹出框
}
});