BaseDialog定义
public abstract class BaseDialog extends Dialog {
protected Context context;
protected Unbinder unbinder;
protected View view;
public BaseDialog(@NonNull Context context) {
super(context);
this.context = context;
}
public BaseDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
this.context = context;
bindView();
}
protected void bindView() {
view = LayoutInflater.from(context).inflate(setContentView(), null);
setContentView(view);
unbinder = ButterKnife.bind(this, view);
}
protected abstract int setContentView();
public void cancelOn() {
unbinder.unbind();
cancel();
}
protected void showToast(String msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
protected void showToastLong(String msg) {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
protected void showToast(String msg,int length) {
Toast.makeText(context, msg,length).show();
}
protected int getColor(int colorId){
return context.getResources().getColor(colorId);
}
}
定义底部显示dialog
public abstract class DataCollectBaseDialog extends BaseDialog {
DataCollectCallBack callBack;
public DataCollectBaseDialog(Context context, DataCollectCallBack callBack) {
this(context, R.style.FullScreenDialogTheme);
this.callBack = callBack;
}
public DataCollectBaseDialog(Context context, int themeId) {
super(context, themeId);
}
@Override
protected void bindView() {
super.bindView();
/** 设置弹窗显示宽度
否则在设置ViewGroup.LayoutParams.MATCH_PARENT为LayoutWidth后,可能出现部分内容超出屏幕无法显示
*/
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
params.width = context.getResources().getDisplayMetrics().widthPixels;
view.setLayoutParams(params);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置重心显示为底部
getWindow().setGravity(Gravity.BOTTOM);
// 设置从底部弹出的动画效果
getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
setOnKeyListener(((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK && keyCode == KeyEvent.ACTION_DOWN) {
cancelOn();
}
return false;
}));
}
}