您的位置:首页 > 其它

BottomSheetDialog的使用

2017-02-08 13:56 519 查看
BottomSheetDialog的简单使用

BottomSheetDialog bsd = new MyBottomSheetDialog(this);
bsd.setContentView(R.layout.items);//布局为任意布局
bsd.show();


比较坑的地方是:

1.向下滑动后,再show不出来

当我们设置bottomSheetDialog点击后不new,而是直接show的话,然而当我们会bottomSheetDialog 展开后,我们将BottomSheetDialog划下隐藏后, 再点击展示BottomSheetDialog后,会发现页面只是变暗,BottomsheetDialog未展开,这是由于之前我们划下收缩隐藏BottomSheetDialog后,bottomSheetDialogBehavior的状态为隐藏,再次show之后,系统未恢复bottomSheetDialogBehavior的状态,还是隐藏,所以再次点击后页面只是变暗。(未能解决,暂时只能每次都new)

2.解决BottomSheetDialog状态栏变黑的问题

继承BottomSheetDialog并重写其protected void onCreate(Bundle savedInstanceState)方法

Activity mActivity;
public MyBottomSheetDialog(@NonNull Context context) {
super(context);
mActivity = (Activity) context;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int screenHeight = getScreenHeight(mActivity);
int statusBarHeight = getStatusBarHeight(getContext());
int dialogHeight = screenHeight - statusBarHeight;
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
}

private static int getScreenHeight(Activity activity) {
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
}

private static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
Resources res = context.getResources();
int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = res.getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BottoSheet