BottomSheetDialog底部按钮

  • Post author:
  • Post category:其他



http://www.jianshu.com/u/35167a70aa39

private void showShareDialog(){
    if(mBottomSheetDialog == null){
        mBottomSheetDialog = new MyBottomSheetDialog(getActivity());
        View v=LayoutInflater.from(getActivity()).inflate(R.layout.bottom_sheet_share_dialog,null);
        mBottomSheetDialog.setContentView(v);
        mBottomSheetDialog.setCancelable(true);
        mBottomSheetDialog.setCanceledOnTouchOutside(true);
        //点击事件
        SuperTextView add=V.findViewById(v,R.id.add_menu);
        SuperTextView cancel=V.findViewById(v,R.id.cancel_menu);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "add", Toast.LENGTH_SHORT).show();
                mBottomSheetDialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBottomSheetDialog.dismiss();
            }
        });


        // 解决下滑隐藏dialog 后,再次调用show 方法显示时,不能弹出Dialog
        final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(mBottomSheetDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet));
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    Log.i("BottomSheet","onStateChanged");
                    mBottomSheetDialog.dismiss();
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
    }else{
        mBottomSheetDialog.show();
    }

}

解决状态栏变色


http://blog.csdn.net/aiynmimi/article/details/66969109

public class MyBottomSheetDialog extends BottomSheetDialog {
    private Context context;
    public MyBottomSheetDialog(@NonNull Context context) {
        super(context);
        this.context=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int screenHeight = getScreenHeight((Activity) context);
        int statusBarHeight = getStatusBarHeight(getContext());
        int dialogHeight = screenHeight - statusBarHeight;
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
    }

    private static int getScreenHeight(Activity activity) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }

    private static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
}

转载于:https://my.oschina.net/u/3015461/blog/1186475