将对话框转换为底表

时间:2018-08-06 11:45:32

标签: android dialog bottom-sheet

我正在参加活动。设置对话框主题后,我可以将其设置为对话框。 我想将其设置为BottomSheetDialog。我希望该对话框被视为底部而不是一个简单的对话框。

有什么办法可以直接作为BottomSheet启动startActivity?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

第一次转换:

AlertDialog dialog =
    new AlertDialog.Builder(getContext()).customView(R.layout.dialog_my_loads_finish, false)
        .show();

至:

View view = View.inflate(getActivity(), R.layout.dialog_my_loads_finish, null);
BottomSheetDialog dialog = new BottomSheetDialog(getActivity());
dialog.setContentView(view);

,如果您想为底纸设置特定的高度,则必须将R.layout.dialog_my_load_finish的根更改为CoordinateLayout,并向第一个孩子添加id和app:layer_behavior 然后:

 dialog.setOnShowListener(dialog2 -> {
  View bottomSheet = dialog.findViewById(R.id.dialog_question_confirm_ct);
  BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
  bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
});

最后:dialog.show()

答案 1 :(得分:0)

将此行添加到布局文件中,位于LayoutLinearLayout/Relativelayput)的开头

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

喜欢:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
     app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
     android:id="@+id/sheetLayout"
     ...........>
     ..........
     ..........

</LinearLayout>

现在在您的Java文件中

BottomSheetDialog bottomSheet= new BottomSheetDialog(getActivity());
View sheet = getActivity().getLayoutInflater().inflate(R.layout.sheetLayout, null);
bottomSheet.setContentView(sheet);
bottomSheet.show();