1.两者相同点
1.1 都可以设置对话框
1.2 都可以设置呈现各种弹窗样式
2.两者区别
本质区别:
Dialog是非阻塞式对话框,AlertDialog弹出时后台还可以做其他事情;
popupwindow是阻塞式对话框,popupwindow弹出时程序会等待,在popupwindow结束前程序会一直等待,当调用dismiss方法后popupwindow弹窗关闭程序才向下进行
其他区别:
1.Pop在显示之前必须设置宽高,Dialog无限制
2.点击back的时候,Dialog会消失,Pop默认会响应(除非设置popup.setFocusable(true))
3.Pop默认不会给页面其他部分添加蒙层,而Dialog默认有蒙层
4.PopupWindow没有标题,Dialog默认有标题
5.二者显示的时候都要设置Gravity。如果不设置,Dialog默认是Gravity.CENTER
6.二者都有默认的背景,都可以通过setBackGroundDrawable(new ColorDrawable(android.R.color.transparent))设置透明
7.Dialog通过show()显示弹窗;Pop通过showAtLocation()弹窗
3.代码实现
Dialog实现底部弹窗
public class BottomDialog extends Dialog implements View.OnClickListener {
private Context context;
private TextView tvCall;
private String name;
private String phone_number;
private TextView tv_contact_customer;
private TextView tv_cancel;
public BottomDialog(@NonNull Context context, String name, String phone_number, @StyleRes int themeResId){
super(context, themeResId);
this.context = context;
this.name = name;
this.phone_number = phone_number;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_contact_phone_dialog);//弹窗布局
if (this.getWindow() != null) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.gravity = Gravity.BOTTOM;//底部弹出
layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
//getWindow().setWindowAnimations(R.style.DialogAnimation);//view中添加动画
}
tvCall = findViewById(R.id.call_service);
tv_contact_customer = findViewById(R.id.tv_contact);
tv_cancel = findViewById(R.id.tv_close);
tv_cancel.setOnClickListener(this);
tv_contact_customer.setOnClickListener(this);
tvCall.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.call_service) {
if (onCallSalesListener != null) {
onCallSalesListener.onCallSalesman();
}
}
if (view.getId() == R.id.tv_contact) {
if (onCallSalesListener != null) {
onCallSalesListener.onContactCustomerService();
}
}
if (view.getId() == R.id.tv_close) {
dismiss();
}
}
//监听接口
public interface OnCallSalesListener {
void onCallSalesman();
void onContactCustomerService();
}
private OnCallSalesListener onCallSalesListener;
public void setOnCallSalesListener(OnCallSalesListener onCallSalesListener) {
this.onCallSalesListener = onCallSalesListener;
}
}
调用:
BottomDialog bottomDialog = new BottomDialog(this, "王老五","110", R.style.AlertDialogStyle);
bottomDialog.show();
popupwindow实现底部弹窗
public class BottomPopWindow extends PopupWindow implements View.OnClickListener{
private View mMenuView;
//文本信息
private TextView tv_prompt_info;
private final RelativeLayout rl_close;
private final TextView tv_close;
public BottomPopWindow(final Context context) {
super(context);
mMenuView = View.inflate(context,R.layout.layout_bottom_pop,null);
//提示信息内容
tv_prompt_info = mMenuView.findViewById(R.id.tv_info);
rl_close = mMenuView.findViewById(R.id.rl_close);
rl_close.setOnClickListener(this);
tv_close = mMenuView.findViewById(R.id.tv_close);
tv_close.setOnClickListener(this);
tv_prompt_info.setText("寒塘渡鹤影,冷月葬花魂");
tv_prompt_info.setHighlightColor(context.getResources().getColor(android.R.color.transparent));
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);//设置布局
//设置弹出窗体的宽和高
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//返回键可点击
this.setFocusable(true);
//外部可点
this.setOutsideTouchable(true);
//弹出窗体的背景
this.setBackgroundDrawable(new ColorDrawable(0xAA000000));
//动画效果
this.setAnimationStyle(R.style.DialogAnimation);
//弹窗dismiss时调用
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
//弹窗结束的收尾工作...
}
});
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.rl_close || v.getId()==R.id.tv_close ){
dismiss();
}
}
}
调用:
BottomPopWindow bottomDialog = new BottomPopWindow(this);
bottomDialog.showAtLocation(findViewById(R.id.llParent), Gravity.BOTTOM, 0, 0);