有一种方法可以在 BottomSheetDialogFragment 中使用 AlertDialog 吗?
There is a way to use AlertDialog inside BottomSheetDialogFragment?
我只需要一个带有标题、消息和按钮的警告对话框,但显示在底部 sheet。
从哪里获得这个(没有自定义视图)?
最好的方法是像您所说的那样使用 BottomSheetDialogFragment 并将其设置为您想要的自定义视图,只有标题、消息和按钮
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
而不是在 bottomsheetdialogfragment 中使用 AlertDialog。
创建一个符合您要求的 bottomsheetdialog。
请参考
https://medium.com/glucosio-project/moving-from-dialogs-to-bottomsheetdialogs-on-android-15fb8d140295
How to use BottomSheetDialog?
AlertDialog
和 BottomSheetDialog
都扩展了 AppCompatDialog
,但有 不同的实现方式。
由于布局很简单(只有标题、消息和按钮),使用带有自定义布局的 BottomSheetDialog
比使用 AlertDialog
并调整 BottomSheet 的所有行为和动画更容易.
只需使用 BottomSheetDialogFragment
(创建 BottomSheetDialog
):
public class MyBottomSheetDialog extends BottomSheetDialogFragment {
@Nullable @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//Use your custom layout
View view = inflater.inflate(R.layout.yourLayout, container, false);
return view;
}
}
然后
MyBottomSheetDialog myBottomSheetDialog = new MyBottomSheetDialog();
myBottomSheetDialog.show(getSupportFragmentManager(), "TAG");
我回答我的问题:不,你被迫使用 CustomView
我只需要一个带有标题、消息和按钮的警告对话框,但显示在底部 sheet。
从哪里获得这个(没有自定义视图)?
最好的方法是像您所说的那样使用 BottomSheetDialogFragment 并将其设置为您想要的自定义视图,只有标题、消息和按钮
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
而不是在 bottomsheetdialogfragment 中使用 AlertDialog。 创建一个符合您要求的 bottomsheetdialog。
请参考 https://medium.com/glucosio-project/moving-from-dialogs-to-bottomsheetdialogs-on-android-15fb8d140295 How to use BottomSheetDialog?
AlertDialog
和 BottomSheetDialog
都扩展了 AppCompatDialog
,但有 不同的实现方式。
由于布局很简单(只有标题、消息和按钮),使用带有自定义布局的 BottomSheetDialog
比使用 AlertDialog
并调整 BottomSheet 的所有行为和动画更容易.
只需使用 BottomSheetDialogFragment
(创建 BottomSheetDialog
):
public class MyBottomSheetDialog extends BottomSheetDialogFragment {
@Nullable @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//Use your custom layout
View view = inflater.inflate(R.layout.yourLayout, container, false);
return view;
}
}
然后
MyBottomSheetDialog myBottomSheetDialog = new MyBottomSheetDialog();
myBottomSheetDialog.show(getSupportFragmentManager(), "TAG");
我回答我的问题:不,你被迫使用 CustomView