在Android中,bringToFront无法正常工作

时间:2016-06-01 16:32:25

标签: android android-layout view android-view android-button

我创建了一个自定义视图,并在其上放置了大约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;
}

}

1 个答案:

答案 0 :(得分:1)

问题可能是由于$settings = Settings::instance(); $this->ogFbAdmins = $settings->str_og_fb_admins; foreach ($this->ogFbAdmins as $adm) { echo $adm->str_og_fb_admins; } 参数(它出现在&gt; = elevation版本上)。

试试这个:

  1. 在您的所有Lollipop
  2. 上设置ViewCompat.setElevation(btn, 0);
  3. 当你想将Buttons移到其他人之上时,不仅而是 btn.bringToFront()