显示
显示的方式是显示一个全局的 AlertDialog ,这个里面也是有一些坑的
private void showAlertAdDialg(final AdvertisementResponse.Advertisement advertisement) {
final Dialog dialog = new Dialog(this, R.style.fullscreen_dialog);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_alert_ad, null, false);
View ivClose = dialogView.findViewById(R.id.iv_alert_ad_close);
ImageView ivOpenAd = (ImageView) dialogView.findViewById(R.id.iv_alert_ad_content);
if (!TextUtils.isEmpty(advertisement.getPictureUrl())) {
Glide.with(this)
.load(advertisement.getPictureUrl())
.into(ivOpenAd);
}
dialogView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ivClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ivOpenAd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ComWebActivity.class);
intent.putExtra(INTENT_KEY_HTML_TITLE, advertisement.getDetailTitle());
intent.putExtra(INTENT_KEY_HTML_URL, advertisement.getDetailUrl());
MainActivity.this.startActivity(intent);
}
});
dialog.setContentView(dialogView);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(lp);
dialog.show();
}
上面这段方法主要做了几件事情:
创建了一个全屏背景半透明的 Dialog,这个全屏主要是通过样式和设置 Dialog 的 WindowAttribute实现的
样式:
true
true
true
false
@android:color/transparent
true
设置为全屏大小:
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(lp);
加载布局,加载图片到 ImageView ,并设置到 Dialog 内
显示 Dialog