bringToFront 在 android 中无法正常工作

bringToFront doesn't work correctly in android

我创建了一个自定义视图并在上面放置了大约 14 个 EditText。我想改变其中一个 EditText 的位置,所以一开始我使用 editText.bringToFront(); 然后我用这个 EditText getChildAt(childIndex).setY(y); 的索引改变了这个按钮的位置。当我看到他们的索引高于此按钮的所有 EditTexts 都在移动时,我感到很惊讶。我不知道为什么 bringToFront 方法会更改其他 EditTexts.

的索引

使用 Button(不是 EditText)的简化源代码。这在没有 bringToFront() 的情况下可以正常工作。但我想使用 EditText:

public class CustomView extends ViewGroup{

private int height;
private int childCount;
int chosenIndex;

public CustomView(Context context) {
    super(context);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    childCount = getChildCount();
    height = 200;

    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        Button child = (Button) getChildAt(childIndex);
        child.layout(l, childIndex * height, r, (childIndex + 1) * height);
        child.setText("" + childIndex);
    }
}

public boolean onInterceptTouchEvent(MotionEvent ev) {
    super.onInterceptTouchEvent(ev);

    float y = ev.getY();

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            for (int i = 0; i < childCount; i++) {
                if (getChildAt(i).getY() < y && getChildAt(i).getY() + getChildAt(i).getHeight() > y) {
                    chosenIndex = i;
                }
            }
            break;

        case MotionEvent.ACTION_MOVE:
                // No bringToFront()
                getChildAt(chosenIndex).setY(y);
            break;

        case MotionEvent.ACTION_UP:
                getChildAt(chosenIndex).setY(y);
            break;
    }

    return false;
}

}

带有 EditText 的源代码(问题就在这里,这不适用于 bringToFront() 或没有它)

public class CustomView extends ViewGroup{

private int height;
private int childCount;
int chosenIndex;

public CustomView(Context context) {
    super(context);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    childCount = getChildCount();
    height = 200;

    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        EditText child = (EditText) getChildAt(childIndex);
        child.layout(l, childIndex * height, r, (childIndex + 1) * height);
        int color = Color.rgb(childIndex * 40, 0, 0);
        child.setBackgroundColor(color);
        child.setText("" + childIndex);
    }
}

public boolean onInterceptTouchEvent(MotionEvent ev) {
    super.onInterceptTouchEvent(ev);

    float y = ev.getY();

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            for (int i = 0; i < childCount; i++) {
                if (getChildAt(i).getY() < y && getChildAt(i).getY() + getChildAt(i).getHeight() > y) {
                    chosenIndex = i;
                }
            }
            break;

        case MotionEvent.ACTION_MOVE:
            getChildAt(chosenIndex).bringToFront();   // Here is the problem with BringToFront
            getChildAt(chosenIndex).setY(y);
            break;

        case MotionEvent.ACTION_UP:
            getChildAt(chosenIndex).setY(y);
            break;
    }

    return false;
}

}

问题可能是由于 elevation 参数(它出现在 >= Lollipop 版本上)。

试试这个:

  1. 在你所有的 Buttons
  2. 上设置 ViewCompat.setElevation(btn, 0); 当您想移动 Button 高人一等。