在Android中定义RelativeLayout视图的Z顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2614393/
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 06:33:22  来源:igfitidea点击:

Defining Z order of views of RelativeLayout in Android

androidandroid-relativelayoutz-order

提问by hpique

I would like to define the z order of the views of a RelativeLayout in Android.

我想在 Android 中定义一个 RelativeLayout 的视图的 z 顺序。

I know one way of doing this is calling bringToFront.

我知道这样做的一种方法是调用bringToFront.

Is there are better way of doing this? It would be great if I could define the z order in the layout xml.

有没有更好的方法来做到这一点?如果我可以在布局 xml 中定义 z 顺序,那就太好了。

回答by Steve Haley

The easiest way is simply to pay attention to the order in which the Views are added to your XML file. Lower down in the file means higher up in the Z-axis.

最简单的方法就是注意将视图添加到 XML 文件的顺序。在文件中较低意味着在 Z 轴上较高。

Edit: This is documented hereand hereon the Android developer site. (Thanks @flightplanner)

编辑:这在 Android 开发人员网站上的此处此处都有记录。(感谢@flightplanner)

回答by QAMAR

If you want to do this in code you can do

如果你想在代码中做到这一点,你可以这样做

View.bringToFront();

see docs

查看文档

回答by Daniel Wilson

Please note, buttons and other elements in API 21 and greater have a high elevation, and therefore ignore the xml order of elements regardless of parent layout. Took me a while to figure that one out.

请注意,API 21 及更高版本中的按钮和其他元素具有较高的高度,因此无论父布局如何,都会忽略元素的 xml 顺序。我花了一段时间才弄明白。

回答by lejonl

In Android starting from API level 21, items in the layout file get their Z order both from how they are ordered within the file, as described in correct answer, and from their elevation, a higher elevation value means the item gets a higher Z order.

在从 API 级别 21 开始的 Android 中,布局文件中的项目根据它们在文件中的排序方式(如正确答案中所述)以及从它们的高度获得其 Z 顺序,较高的高度值意味着项目获得较高的 Z 顺序.

This can sometimes cause problems, especially with buttons that often appear on top of items that according to the order of the XML should be below them in Z order. To fix this just set the android:elevationof the the items in your layout XML to match the Z order you want to achieve.

这有时会导致问题,特别是对于经常出现在项目顶部的按钮,根据 XML 的顺序应该在 Z 顺序中位于它们下方。要解决此问题,只需将android:elevation布局 XML 中的项目设置为匹配您想要实现的 Z 顺序。

I you set an elevation of an element in the layout it will start to cast a shadow. If you don't want this effect you can remove the shadow with code like so:

如果您在布局中设置元素的高度,它将开始投射阴影。如果您不想要这种效果,您可以使用如下代码移除阴影:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   myView.setOutlineProvider(null);
}

I haven't found any way to remove the shadow of a elevated view through the layout xml.

我还没有找到任何方法来通过布局 xml 去除高架视图的阴影。

回答by Tony Vu

I encountered the same issues: In a relative layout parentView, I have 2 children childView1 and childView2. At first, I put childView1 above childView2 and I want childView1 to be on top of childView2. Changing the order of children views did not solve the problem for me. What worked for me is to set android:clipChildren="false"on parentView and in the code I set:

我遇到了同样的问题:在相对布局 parentView 中,我有 2 个孩子 childView1 和 childView2。首先,我将 childView1 放在 childView2 之上,并且我希望 childView1 位于 childView2 之上。改变儿童视图的顺序并没有解决我的问题。对我有用的是在 parentView 和我设置的代码中设置android:clipChildren="false"

childView1.bringToFront();

parentView.invalidate();

回答by Vadim Kotov

Please note that you can use view.setZ(float)starting from API level 21. Hereyou can find more info.

请注意,您可以view.setZ(float)从 API 级别 21 开始使用。在这里您可以找到更多信息。

回答by ThePartyTurtle

Thought I'd add an answer since the introduction of the

自从引入以来,我想我会添加一个答案

android:translationZ

机器人:翻译Z

XML field changed things a tad. The other answers that suggest running

XML 字段稍微改变了一些事情。建议跑步的其他答案

childView1.bringToFront();

parentView.invalidate();

are totally spot on EXCEPT for that this code will NOT bring childView1 in front of any view with a hardcoded android:translationZ in the XML file. I was having problems with this, and once I removed this field from the other views, bringToFront() worked just fine.

除了这个代码不会将 childView1 放在任何带有硬编码 android:translationZ 在 XML 文件中的视图前面。我遇到了这个问题,一旦我从其他视图中删除了这个字段,bringToFront() 工作得很好。

回答by Qamar

API 21 has view.setElevation(float)build-in

API 21 已view.setElevation(float)内置

Use ViewCompat.setElevation(view, float);for backward compatibility

使用ViewCompat.setElevation(view, float);的向后兼容性

More methods ViewCompat.setZ(v, pixels)and ViewCompat.setTranslationZ(v, pixels)

更多方法ViewCompat.setZ(v, pixels)ViewCompat.setTranslationZ(v, pixels)

Another way collect buttons or view array and use addViewto add to RelativeLayout

另一种方法收集按钮或视图数组并用于addView添加到RelativeLayout

回答by kip2

childView.bringToFront() didn't work for me, so I set the Z translation of the least recently added item (the one that was overlaying all other children) to a negative value like so:

childView.bringToFront() 对我不起作用,所以我将最近最少添加的项目(覆盖所有其他孩子的项目)的 Z 转换设置为负值,如下所示:

lastView.setTranslationZ(-10);

lastView.setTranslationZ(-10);

see https://developer.android.com/reference/android/view/View.html#setTranslationZ(float)for more

有关更多信息,请参阅https://developer.android.com/reference/android/view/View.html#setTranslationZ(float)

回答by Petrov Dmitrii

You can use custom RelativeLayoutwith redefined

您可以使用自RelativeLayout定义重新定义

protected int getChildDrawingOrder (int childCount, int i)

protected int getChildDrawingOrder (int childCount, int i)

Be aware - this method takes param ias "which view should I draw i'th". This is how ViewPagerworks. It sets custom drawing order in conjuction with PageTransformer.

请注意 - 此方法将参数i作为“我应该绘制哪个视图i'th”。这就是ViewPager工作原理。它结合PageTransformer.