具体代码 复制使用
1,新建一个BottomBaseStationDialogLayout.java类继承BottomSheetDialogFragment
public class BottomBaseStationDialogLayout extends BottomSheetDialogFragment {
public static BottomBaseStationDialogLayout getInstance() {return new BottomBaseStationDialogLayout(); }@NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {return new BottomSheetDialog(this.getContext()); }
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {mContext = getContext();Log.e("TAG", "onCreateView: ");view = inflater.inflate(R.layout.bottombasestation_dialog_layout, container,false);initViews(view);initData();return view; } @Override public void onStart() {Log.e("TAG", "onStart: ");super.onStart();//获取dialog对象BottomSheetDialog dialog = (BottomSheetDialog) getDialog();//把windowsd的默认背景颜色去掉,不然圆角显示不见dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//获取diglog的根部局FrameLayout bottomSheet = dialog.getDelegate().findViewById(R.id.design_bottom_sheet);if (bottomSheet != null) {//获取根部局的LayoutParams对象CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();layoutParams.height = getPeekHeight();//修改弹窗的最大高度,不允许上滑(默认可以上滑)bottomSheet.setLayoutParams(layoutParams);final BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);//peekHeight即弹窗的最大高度behavior.setPeekHeight(getPeekHeight());int height = (int) (getPeekHeight() * 0.8);LogUtils.i(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> " + height);// 初始为展开状态behavior.setState(BottomSheetBehavior.STATE_EXPANDED);//关闭弹窗 // behavior.setState(BottomSheetBehavior.STATE_HIDDEN);} } private void initViews(View view) { //这里进行控件的初始化 } private void initData() {//这里可以进行数据的初始化}
/***这里设置view显示的高度,如果不设置BottomSheetDialog默认是显示全屏* @return height*/ protected int getPeekHeight() {int peekHeight = getResources().getDisplayMetrics().heightPixels;//设置弹窗高度为屏幕高度的比例return peekHeight - peekHeight / 10; }
}
2,如何调用 使用 在MainActivity.java中
public class MainActivity extends Activity{ @Override public void onCreate() {//点击按钮调用 BottomBaseStationDialogLayout fragment = new BottomBaseStationDialogLayout(); fragment.show(this, "ssss");} }