单击靠近其边缘时,dialogFragment 不会关闭

dialogFragment doesn't dismiss when clicking close to it's edge

我有一个基本上可以工作的自定义 dialogFragment,但是只有当您在远离对话框的地方单击(即点击)时它才会消失。如果我在非常靠近对话框的地方单击,但仍在外面(例如,距离边缘 30 像素),什么也不会发生...对话框不会关闭。

我发现即使在没有自定义的基本 alertDialog 上也会出现这种情况。据我所知,这是标准的 Android 事情。我错了吗?有什么原因吗?

有一个属性.setCanceledOnTouchOutside();所做的更改确实会按预期影响工作中的点击远距离解雇,但不会影响上述接近边缘的情况。

对话框class:

public class Filters_DialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.filters_dialog, container, false);
        getDialog().setTitle("Simple Dialog");

        // FYI, this has no affect on clicking very close to the dialog edge.
        getDialog().setCanceledOnTouchOutside(true);

        return rootView;
    }

}

对话框布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#333333">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FILTERS"
        android:textColor="#ffffff" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

函数在我的 activity 调用对话框中:

private void showFiltersDialog() {

    FragmentManager fm = getSupportFragmentManager();
    Filters_DialogFragment dialogFragment = new Filters_DialogFragment();
    dialogFragment.show(fm, "Sample Fragment");

}

我自己也遇到过这个问题,因此对源代码进行了一些挖掘。原来这是故意的行为,被称为 "touchSlop"。它在 ViewConfiguration 中定义:

https://developer.android.com/reference/android/view/ViewConfiguration.html#getScaledWindowTouchSlop()

违规代码在 Window class:

public boolean shouldCloseOnTouch(Context context, MotionEvent event) {
        if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN
                && isOutOfBounds(context, event) && peekDecorView() != null) {
            return true;
        }
        return false;
    }

然后调用:

private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getDecorView();
        return (x < -slop) || (y < -slop)
                || (x > (decorView.getWidth()+slop))
                || (y > (decorView.getHeight()+slop));
    }

其中的值来自:

/**
     * Distance in dips a touch needs to be outside of a window's bounds for it to
     * count as outside for purposes of dismissing the window.
     */
    private static final int WINDOW_TOUCH_SLOP = 16;

我找不到任何方法来覆盖此行为或更改倾斜值。我认为唯一的选择是实现一个具有透明背景和手动点击处理程序的全屏对话框。我已经决定让我的应用覆盖默认系统行为不是一个好主意,所以我不打算实现它。