在Android中定义RelativeLayout视图的Z顺序
我想在Android 中定义RelativeLayout 视图的z 顺序。
我知道一种方法是调用 bringToFront
。
有更好的方法吗?如果我可以在布局 xml 中定义 z 顺序,那就太好了。
I would like to define the z order of the views of a RelativeLayout in Android.
I know one way of doing this is calling bringToFront
.
Is there are better way of doing this? It would be great if I could define the z order in the layout xml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
最简单的方法就是注意视图添加到 XML 文件的顺序。文件中较低的位置意味着 Z 轴较高的位置。
编辑:
此处 和 此处。 (感谢@flightplanner)
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.
Edit:
This is documented here and here on the Android developer site. (Thanks @flightplanner)
如果你想在代码中执行此操作
你可以
看到文档
If you want to do this in code
you can do
see docs
请注意,API 21 及更高版本中的按钮和其他元素具有较高的标高,因此无论父布局如何,都会忽略元素的 xml 顺序。我花了一段时间才弄清楚这一点。
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 开始的 Android 中,布局文件中的项目从文件中的排序方式(如正确答案中所述)获取其 Z 顺序,并从其标高获取,较高的标高值意味着该项目获得更高的 Z 顺序。
这有时会导致问题,特别是对于经常出现在项目顶部的按钮,根据 XML 的顺序,这些按钮按 Z 顺序应位于项目的下方。要解决此问题,只需在布局 XML 中设置项目的
android:elevation
以匹配您想要实现的 Z 顺序。如果您在布局中设置元素的高度,它将开始投射阴影。如果您不想要这种效果,可以使用如下代码删除阴影:
我还没有找到任何方法可以通过布局 xml 删除提升视图的阴影。
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.
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:elevation
of the the items in your layout XML to match the Z order you want to achieve.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:
I haven't found any way to remove the shadow of a elevated view through the layout xml.
请注意,从 API 级别 21 开始,您可以使用
view.setZ(float)
。 此处您可以找到更多信息。Please note that you can use
view.setZ(float)
starting from API level 21. Here you can find more info.我遇到了同样的问题:在相对布局parentView中,我有2个子childView1和childView2。首先,我将childView1放在childView2之上,并且我希望childView1位于childView2之上。更改儿童视图的顺序并没有解决我的问题。对我有用的是在parentView 和我设置的代码中设置 android:clipChildren="false" :
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:
自从引入以来我想添加一个答案
XML 字段稍微改变了一些事情。建议运行的其他答案
完全正确,除了此代码不会将 childView1 带到任何在 XML 文件中带有硬编码 android:translationZ 的视图前面。我在这方面遇到了问题,一旦我从其他视图中删除了该字段,bringToFront() 就可以正常工作了。
Thought I'd add an answer since the introduction of the
XML field changed things a tad. The other answers that suggest running
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.
API 21 内置
view.setElevation(float)
使用
ViewCompat.setElevation(view, float);
实现向后兼容性更多方法
ViewCompat.setZ(v, Pixels)
和 ViewCompat.setTranslationZ(v, Pixels)另一种收集按钮或视图数组并使用
addView
添加到relativelayout的方法API 21 has
view.setElevation(float)
build-inUse
ViewCompat.setElevation(view, float);
for backward compatibilityMore methods
ViewCompat.setZ(v, pixels)
andViewCompat.setTranslationZ(v, pixels)
Another way collect buttons or view array and use
addView
to add to RelativeLayoutchildView.bringToFront() 对我不起作用,因此我将最近添加的项目(覆盖所有其他子项的项目)的 Z 平移设置为负值,如下所示:
lastView.setTranslationZ(-10 );
请参阅 https://developer.android .com/reference/android/view/View.html#setTranslationZ(float) 了解更多
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:
lastView.setTranslationZ(-10);
see https://developer.android.com/reference/android/view/View.html#setTranslationZ(float) for more
或者将重叠的按钮或视图放入 FrameLayout 内。然后,xml 文件中的relativelayout 将遵循子布局添加的顺序。
Or put the overlapping button or views inside a FrameLayout. Then, the RelativeLayout in the xml file will respect the order of child layouts as they added.
您可以使用自定义
RelativeLayout
和重新定义的protected int getChildDrawingOrder (int childCount, int i)
请注意 - 此方法将参数
i
作为“我应该绘制哪个视图第i
”。这就是 ViewPager 的工作原理。它与
PageTransformer
结合设置自定义绘制顺序。You can use custom
RelativeLayout
with redefinedprotected int getChildDrawingOrder (int childCount, int i)
Be aware - this method takes param
i
as "which view should I drawi'th
".This is how
ViewPager
works. It sets custom drawing order in conjuction withPageTransformer
.检查 XML 中的视图之一是否有任何标高。如果是这样,请向其他项目添加标高或删除标高以解决问题。从那里开始,视图的顺序决定了什么优先于另一个。
Check if you have any elevation on one of the Views in XML. If so, add elevation to the other item or remove the elevation to solve the issue. From there, it's the order of the views that dictates what comes above the other.
您也可以使用下面的代码示例来实现相同的功能,
这是向后兼容的。
这里
mCardView
是一个视图,应该位于sourceView
下面。You can use below code sample also for achieving the same
This is backward compatible.
Here
mCardView
is a view which should be belowsourceView
.