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

发布于 2024-08-28 13:24:48 字数 135 浏览 10 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

魔法唧唧 2024-09-04 13:24:48

最简单的方法就是注意视图添加到 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)

乖乖 2024-09-04 13:24:48

如果你想在代码中执行此操作
你可以

View.bringToFront();

看到文档

If you want to do this in code
you can do

View.bringToFront();

see docs

一桥轻雨一伞开 2024-09-04 13:24:48

请注意,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.

深爱成瘾 2024-09-04 13:24:48

在从 API 级别 21 开始的 Android 中,布局文件中的项目从文件中的排序方式(如正确答案中所述)获取其 Z 顺序,并从其标高获取,较高的标高值意味着该项目获得更高的 Z 顺序。

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

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

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

我还没有找到任何方法可以通过布局 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:

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.

东京女 2024-09-04 13:24:48

请注意,从 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.

人│生佛魔见 2024-09-04 13:24:48

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

childView1.bringToFront();

parentView.invalidate();

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:

childView1.bringToFront();

parentView.invalidate();
秋意浓 2024-09-04 13:24:48

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

安卓:翻译Z

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

childView1.bringToFront();

parentView.invalidate();

完全正确,除了此代码不会将 childView1 带到任何在 XML 文件中带有硬编码 android:translationZ 的视图​​前面。我在这方面遇到了问题,一旦我从其他视图中删除了该字段,bringToFront() 就可以正常工作了。

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

android:translationZ

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

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.

可爱咩 2024-09-04 13:24:48

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-in

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

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

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

二手情话 2024-09-04 13:24:48

childView.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

醉南桥 2024-09-04 13:24:48

或者将重叠的按钮或视图放入 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.

依 靠 2024-09-04 13:24:48

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

protected int getChildDrawingOrder (int childCount, int i)

请注意 - 此方法将参数 i 作为“我应该绘制哪个视图 第i”。
这就是 ViewPager 的工作原理。它与 PageTransformer 结合设置自定义绘制顺序。

You can use custom RelativeLayout with redefined

protected int getChildDrawingOrder (int childCount, int i)

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

摇划花蜜的午后 2024-09-04 13:24:48

检查 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.

逆光下的微笑 2024-09-04 13:24:48

您也可以使用下面的代码示例来实现相同的功能,

ViewCompat.setElevation(sourceView, ViewCompat.getElevation(mCardView)+1);

这是向后兼容的。
这里mCardView是一个视图,应该位于sourceView下面。

You can use below code sample also for achieving the same

ViewCompat.setElevation(sourceView, ViewCompat.getElevation(mCardView)+1);

This is backward compatible.
Here mCardView is a view which should be below sourceView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文