一个自定义的DialogBuilder
项目暂时告一段落,记录一下,一个自定义的dialogBuilder,省去很多重复代码
自定义DialogBuilder
/**
* Author:roc
* Date: 2018/8/3 下午4:20
* Desc: 自定义dialogBuilder类
*/
public abstract class BaseDialogBuilder {
Context mContext;
public BaseDialogBuilder(Context mContext) {
this.mContext = mContext;
}
public Dialog create() {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Dialog dialog = new Dialog(mContext, R.style.custom_dialog);
View layout = inflater.inflate(getLayoutId(), null);
dialog.addContentView(layout, new WindowManager.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT
, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
dialog.setContentView(layout);
// 点击外部区域是否消失
dialog.setCanceledOnTouchOutside(setCanceledOnTouchOutside());
//获得dialog的window窗口
Window window = dialog.getWindow();
//设置Dialog底背景暗淡
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//设置dialog在屏幕底部
window.setGravity(setGravity());
//设置dialog弹出时的动画效果,淡入淡出效果
window.setWindowAnimations(R.style.appearStyle);
window.getDecorView().setPadding(0, 0, 0, 0);
//获得window窗口的属性
WindowManager.LayoutParams lp = window.getAttributes();
//设置dialog自身黑暗度
lp.dimAmount = 0.5f;
//设置窗口宽度为充满全屏
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
//设置窗口高度为包裹内容
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//将设置好的属性set回去
window.setAttributes(lp);
initView(layout);
setData(dialog);
return dialog;
}
/**
* 布局
*
* @return
*/
abstract int getLayoutId();
/**
* 初始化View
*
* @param view
*/
abstract void initView(View view);
/**
* 设置数据
*
* @param dialog
*/
abstract void setData(Dialog dialog);
/**
* 显示方式
* Gravity.BOTTOM
* Gravity.TOP
* Gravity.CENTER
*
* @return
*/
abstract int setGravity();
/**
* 点击外部区域是否消失
*
* @return
*/
abstract boolean setCanceledOnTouchOutside();
}
style文件
<!-- 自定义dialog的style样式 -->
<style name="custom_dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
<!-- 弹出框动画 淡入淡出 -->
<style name="appearStyle" parent="@android:style/Animation.Dialog">
<!-- 进入时的动画 -->
<item name="android:windowEnterAnimation">@anim/dialog_appear</item>
<!-- 退出时的动画 -->
<item name="android:windowExitAnimation">@anim/dialog_disappear</item>
</style>
dialog_appear和dialog_disappear是两个淡入淡出的动画文件
使用方式
public class CustomeDialog {
public interface OnLeftClickLisntener {
void onLeftClick();
}
public interface OnRightClickListener {
void onRightClick();
}
public static class Builder extends BaseDialogBuilder {
ImageView leftBtn, rightBtn;
OnLeftClickLisntener onLeftClickLisntener;
OnRightClickListener onRightClickListener;
public Builder setOnLeftClickLisntener(OnLeftClickLisntener onLeftClickLisntener) {
this.onLeftClickLisntener = onLeftClickLisntener;
return this;
}
public Builder setOnRightClickLisntener(OnRightClickListener onRightClickListener) {
this.onRightClickListener = onRightClickListener;
return this;
}
public Builder(Context mContext) {
super(mContext);
}
@Override
int getLayoutId() {
return R.layout.dialog_layout;
}
@Override
void initView(View view) {
leftBtn = view.findViewById(R.id.prevMonth);
rightBtn = view.findViewById(R.id.nextMonth);
}
@Override
void setData(Dialog dialog) {
currentMonth.setText(titlDate);
leftBtn.setOnClickListener(v -> {
if (onLeftClickLisntener != null) {
onLeftClickLisntener.onLeftClick();
}
});
rightBtn.setOnClickListener(v -> {
if (onRightClickListener != null) {
onRightClickListener.onRightClick();
}
});
}
@Override
int setGravity() {
return Gravity.BOTTOM;
}
@Override
boolean setCanceledOnTouchOutside() {
return true;
}
}
网友评论