Dim Screen and Block Interaction with BottomSheets

为君一笑 提交于 2020-01-02 02:41:09

问题


BottomSheetBehavior has been introduced in Android Design Support Library 23.2, however it does not dim the rest of the screen and does not block interaction with the rest of the UI. Is there anyway this can be achieved?


回答1:


public class BottomSheetDimmedFragment extends BottomSheetDialogFragment {
    public static final String TAG = BottomSheetDimmedFragment.class.getSimpleName();

    @NonNull
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        final BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        final View view = View.inflate(getContext(), R.layout.test, null);
        dialog.setContentView(view);
        return dialog;
    }

    public void show(final FragmentActivity fragmentActivity) {
        show(fragmentActivity.getSupportFragmentManager(), TAG);
    }
}

In your activity:

BottomSheetDimmedFragment sheet = new BottomSheetDimmedFragment();
sheet.show(this);

Now, you will have a dim and also when clicked on a dim the dialog will close.

Implementation taken from here.




回答2:


Use the bottom sheet with a fragment instead of a view :)




回答3:


Note that there are two implementations:

BottomSheetBehavior and BottomSheetDialogFragment.

Use BottomSheetDialogFragment to get the functionality you need.

Also when using BottomSheetBehavior set the layout's android:clickable="true". That way clicks don't go through when you click on empty space. (For clarity: clickable is set on the layout containing the tag app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior")



来源:https://stackoverflow.com/questions/37596701/dim-screen-and-block-interaction-with-bottomsheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!