Android 设置 View 的 z 顺序使用 becomeChildToFront()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3740100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:36:52  来源:igfitidea点击:

setting z order of View with bringChildToFront()

android

提问by rob

I'm trying to set the z order of a UI element (i.e. a View) so that it will overlap another element, but calling ViewGroup.bringChildToFront() has a weird side effect....it moves the element to be the last item in the parent (the ViewGroup). Is this a bug, expected behavior, or what? More importantly, how can I set the z order or a View without this unfortunate side effect?

我正在尝试设置 UI 元素(即视图)的 z 顺序,以便它会与另一个元素重叠,但是调用 ViewGroup.bringChildToFront() 有一个奇怪的副作用......它将元素移动到最后一个父项(ViewGroup)中的项。这是错误,预期行为还是什么?更重要的是,如何设置 z 顺序或视图而不会有这种不幸的副作用?

回答by Romain Guy

This is the expected behavior. bringChildToFront() just changes the index of the View inside its parent.

这是预期的行为。BringChildToFront() 只是更改其父视图内的视图索引。

回答by Kumar

To send a view all the way back, do this:

要一直发送视图,请执行以下操作:

private void moveToBack(View currentView) {
    ViewGroup viewGroup = ((ViewGroup) currentView.getParent());
    int index = viewGroup.indexOfChild(currentView);
    for(int i = 0; i<index; i++) {
        viewGroup.bringChildToFront(viewGroup.getChildAt(i));
    }
}

回答by sm abbas

bringChildToFront(child)does nothing but changes the index value of the child.

bringChildToFront(child)除了改变孩子的索引值之外什么都不做。

In order to bring a child to the front without using showNext()or showprevious(),

为了在不使用showNext()或的情况下将孩子带到前面showprevious()

use

setDisplayedChild()and indexOfChild()together.

setDisplayedChild()indexOfChild()在一起。

example

例子

vf.setDisplayedChild(vf.indexOfChild(child));

vf.setDisplayedChild(vf.indexOfChild(child));

where childis the viewthat needs to be brought front.

其中child是需要放在前面的视图

Thanks

谢谢