Android 基礎(chǔ)彈框DataBinding+DialogFragment的使用封裝
針對目前彈框的使用比較頻繁囱淋,現(xiàn)在動(dòng)手封裝一個(gè)彈框的基類建炫,使用DataBinding+DialogFragment豹芯,希望能夠幫到大家幸海。
1.onCreate()方法中添加Style.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL,R.style.BaseDialogStyle);
}
BaseDialogStyle代碼如下(大家根據(jù)自己的需求進(jìn)行修改):
<style name="BaseDialogStyle" parent="Theme.MaterialComponents.DayNight.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowTranslucentStatus">true</item><!-- 解決全屏?xí)r狀態(tài)欄變黑 -->
<item name="android:backgroundDimEnabled">true</item> <!-- 是否允許背景模糊 -->
<item name="android:windowIsTranslucent">true</item> <!-- 是否半透明 -->
<item name="android:windowIsFloating">true</item> <!-- 是否浮現(xiàn)在activity之上 -->
<item name="android:windowContentOverlay">@null</item> <!-- 是否有遮蓋 -->
<item name="android:backgroundDimAmount">0.8</item> <!-- 設(shè)置背景模糊的透明度-->
</style>
-
彈框核心代碼:
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { mContext = getActivity(); if (getDialog()!=null) getDialog().setCanceledOnTouchOutside(isOnTouchOutSide); viewBinding = DataBindingUtil.inflate(inflater,layoutResId(),container,false); viewBinding.setLifecycleOwner(this); initData(); return viewBinding.getRoot(); }
3.在onStart()方法中可以動(dòng)態(tài)設(shè)置寬高和透明度以及彈框的位置(上下左右):
@Override
public void onStart() {
super.onStart();
setSite(viewSite);
}
private void setSite(int site) {
Window mWindow = Objects.requireNonNull(getDialog()).getWindow();
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.width = windowWidth;
mLayoutParams.height = windowHeight;
mLayoutParams.gravity = site;
mWindow.setAttributes(mLayoutParams);
mWindow.setDimAmount(alpha);// 0~1 , 1表示完全昏暗
}
4.根據(jù)自己的需求封裝toast,例如:
protected void showToast(CharSequence msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
}
protected void showToast(@StringRes int msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
}
5.完整代碼如下:
@SuppressLint("ValidFragment")
public abstract class BaseDialog<D extends ViewDataBinding> extends DialogFragment {
protected D viewBinding;
protected Context mContext;
private int viewSite = Gravity.CENTER;//布局位置
private boolean isOnTouchOutSide = false;
private float alpha = 0.8f;
private int windowWidth = ViewGroup.LayoutParams.MATCH_PARENT;
private int windowHeight = ViewGroup.LayoutParams.MATCH_PARENT;
public BaseDialog() {
super();
}
/**
* 是否點(diǎn)擊外部消失
* @param isOnTouchOutSide
*/
public BaseDialog(boolean isOnTouchOutSide) {
this.isOnTouchOutSide = isOnTouchOutSide;
}
/**
* 布局位置
* @param site
*/
public BaseDialog(int site) {
viewSite = site;
}
public BaseDialog(int site, boolean isOnTouchOutSide) {
this(isOnTouchOutSide);
viewSite = site;
}
public BaseDialog(float alpha, int windowWidth, int windowHeight) {
this.alpha = alpha;
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
}
public BaseDialog(int site, int windowWidth, int windowHeight) {
this.viewSite = site;
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
}
protected abstract @LayoutRes
int layoutResId();
protected abstract void initData();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL,R.style.BaseDialogStyle);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
mContext = getActivity();
if (getDialog()!=null){
getDialog().setCanceledOnTouchOutside(isOnTouchOutSide);
//禁止返回關(guān)閉彈框可在此處處理
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
// @Override
// public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// if (keyCode == 4) return true;
// Log.i(TAG, "onKey: "+keyCode);
// return false;
// }
// });
// }
}
viewBinding = DataBindingUtil.inflate(inflater,layoutResId(),container,false);
viewBinding.setLifecycleOwner(this);
initData();
return viewBinding.getRoot();
}
public boolean isOnTouchOutSide() {
return isOnTouchOutSide;
}
public BaseDialog setOnTouchOutSide(boolean onTouchOutSide) {
isOnTouchOutSide = onTouchOutSide;
if (getDialog() != null) {
getDialog().setCanceledOnTouchOutside(onTouchOutSide);
}
return this;
}
public int getViewSite() {
return viewSite;
}
public BaseDialog setViewSite(int viewSite) {
this.viewSite = viewSite;
setSite(viewSite);
return this;
}
@Override
public void onStart() {
super.onStart();
setSite(viewSite);
}
private void setSite(int site) {
Window mWindow = Objects.requireNonNull(getDialog()).getWindow();
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.width = windowWidth;
mLayoutParams.height = windowHeight;
mLayoutParams.gravity = site;
mWindow.setAttributes(mLayoutParams);
mWindow.setDimAmount(alpha);// 0~1 , 1表示完全昏暗
}
protected void showToast(CharSequence msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
// ToastUtils.showShort(mContext,msg);
}
protected void showToast(@StringRes int msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
// ToastUtils.showShort(mContext,msg);
}
最后站蝠,希望對大家有所幫助揽祥,如果有不對的地方,歡迎大家指正桨醋。謝謝棚瘟!