public abstract class BaseBottomDialog extends DialogFragment {
private static final String TAG = "dialog";
private static final float DEFAULT_DIM = 0.2f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BottomDialog);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 解决虚拟按键遮挡的问题
getDialog().setCanceledOnTouchOutside(getCancelOutside());
View v = inflater.inflate(getLayoutRes(), container, false);
bindView(v);
return v;
}
@LayoutRes
public abstract int getLayoutRes();
public abstract void bindView(View v);
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.dimAmount = getDimAmount();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
if (getHeight() > 0) {
params.height = getHeight();
} else {
//params.height = WindowManager.LayoutParams.WRAP_CONTENT;
// params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.height = getScreenHeight() - getStatusBarHeight();
}
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
}
public int getHeight() {
return -1;
}
public float getDimAmount() {
return DEFAULT_DIM;
}
public boolean getCancelOutside() {
return true;
}
public String getFragmentTag() {
return TAG;
}
public void show(FragmentManager fragmentManager) {
show(fragmentManager, getFragmentTag());
}
/**
* 获取屏幕高度
*
* @return 屏幕高度
*/
public int getScreenHeight() {
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getHeight();
}
/**
* 获取状态栏高度
*
* @return 状态栏高度
*/
public int getStatusBarHeight() {
int result = 0;
Rect rectangle= new Rect();
getDialog().getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
//高度为rectangle.top-0仍为rectangle.top
Log.e("WangJ", "状态栏-方法3:" + rectangle.top);
return result;
}
}