如何在BottomSheetDialog中更改透明背景?

时间:2018-07-12 09:30:03

标签: android

我正在使用BottomSheetDialog,但出现了一些困难。我要删除黑色背景的背景,并将其更改为透明度。我尝试了此BottomSheetDialog with transparent background,但没有成功。救救我。

[在此处输入图片描述] [1]

代码是:

BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getActivity());
    View parentView = getLayoutInflater().inflate(R.layout.content_status_dialog,null);
    bottomSheetDialog.setContentView(parentView);
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View)parentView.getParent());
    bottomSheetDialog.setCancelable(true);
    bottomSheetBehavior.setPeekHeight((int)TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,100,
            getResources().getDisplayMetrics()));

    bottomSheetDialog.show();

如果我添加这样的样式

 ((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);

,看起来像这样

2 个答案:

答案 0 :(得分:2)

检查以下解决方案是否有碎片:

public class BottomSheetDialogClass extends BottomSheetDialogFragment {
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.fragment_bottom_sheet, null);
        dialog.setContentView(contentView);

        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent())
                .getLayoutParams();
        CoordinatorLayout.Behavior behavior = params.getBehavior();
        ((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }
}

答案 1 :(得分:1)

唯一需要做的就是添加此行bottomSheetDialog.getWindow()。setDimAmount(0);

在bottomSheetDialog.show();

之前