BottomSheetDialog 的两个坑

来源:互联网 发布:淘宝买日系钢笔的店铺 编辑:程序博客网 时间:2024/05/18 03:48

本人在使用 BottomSheetDialog 的时候发现了两个问题,幸运的是都已经有人解决了,下面会给出链接

  • 第一个坑:通过滑动关闭 BottomSheetDialog 后再 show() ,Dialog 中的内容却没有显示出来
    http://blog.csdn.net/yanzhenjie1003/article/details/51938400
  • 第二个坑:BottomSheetDialog 显示的时候状态栏变成黑色
    http://www.jianshu.com/p/8d43c222b551

下面为了方便解决问题会把代码贴出来,如果想要了解原理或者更多内容请移步至上述的文章

第一个坑:

private void setBehaviorCallback() {    View view = mBottomSheetDialog.getDelegate().findViewById(        android.support.design.R.id.design_bottom_sheet);    final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(view);    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {        @Override        public void onStateChanged(@NonNull View bottomSheet, int newState) {            if (newState == BottomSheetBehavior.STATE_HIDDEN) {                mBottomSheetDialog.dismiss();                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);            }        }        @Override        public void onSlide(@NonNull View bottomSheet, float slideOffset) {        }    });}

在 BottomSheetDialog 创建完成后调用该方法即可

第二个坑:

修改 BottomSheetDialog 的高度为 屏幕高度 - 状态栏高度

public class MyBottomSheetDialog extends BottomSheetDialog {    private Context mContext;    public MyBottomSheetDialog(@NonNull Context context) {        super(context);        mContext = context;    }    public MyBottomSheetDialog(@NonNull Context context, @StyleRes int theme) {        super(context, theme);        mContext = context;    }    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        int screenHeight = getScreenHeight((Activity) mContext);        int statusBarHeight = getStatusBarHeight((Activity) mContext);        int dialogHeight = screenHeight - statusBarHeight;        getWindow().setLayout(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;    }}
1 0
原创粉丝点击