Android 视图带到前面不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25306180/
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
View bring to front doesnt work
提问by Lena Bru
I want to change z order of some views during animation
我想在动画期间更改某些视图的 z 顺序
On Androids above 4.1.2 it works just fine, and on androids below 4.1.2 the Z order doesnt change, the top view remains on top.
在 4.1.2 以上的 Android 上它工作得很好,在 4.1.2 以下的 android 上,Z 顺序不会改变,顶视图仍然在顶部。
This is what i am trying.
这就是我正在尝试的。
myView.bringToFront();
((View)myView.getParent()).invalidate();
How to make it work on older devices ?
如何让它在旧设备上工作?
回答by Lena Bru
/**
* Change the view's z order in the tree, so it's on top of other sibling
* views. This ordering change may affect layout, if the parent container
* uses an order-dependent layout scheme (e.g., LinearLayout). Prior
* to {@link android.os.Build.VERSION_CODES#KITKAT} this
* method should be followed by calls to {@link #requestLayout()} and
* {@link View#invalidate()} on the view's parent to force the parent to redraw
* with the new child ordering.
*
* @see ViewGroup#bringChildToFront(View)
*/
public void bringToFront() {
if (mParent != null) {
mParent.bringChildToFront(this);
}
}
according to this I was simply missing the line
据此,我只是错过了这条线
((View)myView.getParent()).requestLayout();
and it worked!
它奏效了!
回答by Colibri
I tried all that. A RelativeLayout was supposed to be on top of a Button but it just didn't want to obey.
我尝试了所有这些。RelativeLayout 应该位于 Button 之上,但它只是不想服从。
In the end I solved it by adding android:elevation="2dp"
to the RelativeLayout.
最后我通过添加android:elevation="2dp"
到RelativeLayout来解决它。
The elevation value is only used in API level 21 and higher. In my case everything below the API level 21 was fine and everything above wasn't.
海拔值仅用于 API 级别 21 及更高级别。在我的情况下,API 级别 21 以下的一切都很好,而上面的一切都没有。
Hope this might help someone :)
希望这可以帮助某人:)
回答by Adrian Buciuman
I hope this will be useful for somebody. None of the above solutions worked for me. Why? Because my view that I wanted to be in the frond had an elevation smaller than other view. So the view with bigger elevation was always in front, no matter what.
我希望这对某人有用。以上解决方案都不适合我。为什么?因为我想站在前面的观点比其他观点的高度要小。所以无论如何,海拔更高的景色总是在前面。
回答by BOTJr.
For Api's 21 or above There is an xml attribute known as translateZ
which lets you define the elevation level on the view.
对于 Api 的 21 或更高版本,有一个 xml 属性称为 xml 属性translateZ
,可让您定义视图上的高程级别。
You can pretty much use that too.
你也几乎可以使用它。
android:translateZ="30dp"
android:translateZ="30dp"
*APIs >=21
*API >=21
回答by kjoelbro
If the parent view is a relativelayout, then it might work or it might not work. I tried bringToFront
, requestLayout
, and removeView; addView
, but no luck. My solution was to put everything inside a framelayout. What I needed on top was moved the buttom of the framelayout with visibility invisible, and then in code it was made visible.
如果父视图是相对布局,那么它可能会工作,也可能不会工作。我试过bringToFront
, requestLayout
, 和removeView; addView
,但没有运气。我的解决方案是将所有内容都放在框架布局中。我最需要的是在可见性不可见的情况下移动框架布局的底部,然后在代码中使其可见。