android 上拉固定高度显示,下拉关闭View BottomSheetDialog

具体代码 复制使用

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");
        }
}

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值