原生控件仿苹果底部抽屉弹框

demo地址:https://github.com/feiyuu/BottomSheetDialogDemo

基本使用很简单:

public class FullSheetDialogFragment extends BottomSheetDialogFragment {
    private BottomSheetBehavior mBehavior;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        View view = View.inflate(getContext(), R.layout.dialog_bottom_sheet, null);
        dialog.setContentView(view);
        mBehavior = BottomSheetBehavior.from((View) view.getParent());
        return dialog;
    }
 
    @Override
    public void onStart()
    {
        super.onStart();
        //默认全屏展开
        mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }
 
    public void doclick(View v)
    {
        //点击任意布局关闭
        mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    }
}

调用BottomSheetDialogFragment展示

new FullSheetDialogFragment().show(getSupportFragmentManager(), "dialog");

下面是一些坑。

dialog按屏幕高度百分比调整大小:
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState); mContext = getContext(); 
View view = View.inflate(mContext, R.layout.dialog_bottom_sheet1, null); dialog.setContentView(view); 
ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); 
int height = (int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.9); layoutParams.height = height; 
view.setLayoutParams(layoutParams);
 
去除层叠显示(就是去掉完全展开,中间状态,隐藏中的中间状态):

height就是整个dialog的高度,同上面的height。如果设置为0也可以实现效果,但会导致滑动关闭后,有一层阴影遮罩,
需要点击一次才能消失。原因是设置为0,滑动到看不见时,其实是处于中间状态,并没有完全关闭dialog。

mBehavior.setPeekHeight(height);
圆角背景:
//先设置背景为透明
 
dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
 
//shape
 


 
    
    
 

你可能感兴趣的:(原生控件仿苹果底部抽屉弹框)