如何将视图发送到后台?如何以编程方式控制 z 顺序?
我在将视图发送回去时遇到问题。在 Android 中,我们有一个类似 bringToFront()
的方法,可以将视图放置在另一个视图之上。像这样,我想把视图放在上一张图片的下面。
Android 中有类似 sendToBack()
或 bringToBack()
的方法吗?如果是这样,任何人都可以帮助我吗?
注意:我不想通过在布局中放置项目的顺序来控制 z 顺序,我想以编程方式控制 z 顺序。
我不想隐藏前面的视图,我只是希望它们位于正在移动的视图后面。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我意识到其他答案中已经暗示了这一点,但没有人发布代码。我将以下内容保留在实用程序类中,其中有用于处理视图的“帮助器”函数:
I realize that this has been implied in other answers, but no one posted the code. I keep the following in a utility class where I have "helper" functions for dealing with views:
没有
sendToBack()
方法。但是,如果您在位置较低的视图上调用bringToFront()
,您将获得几乎相同的效果。There is no
sendToBack()
method. But if you callbringToFront()
on the lower positioned view you get almost the same effect.据我所知,没有内置的解决方案。我猜您正在尝试修改 FrameLayout 中的 z 顺序或类似的内容。
但是,我认为您可以修改布局中包含的子元素的顺序。 RemoveChild...() 和 addView() 方法可以获取位置值,因此您很可能可以交换子元素,这会修改 z 顺序。然而,这似乎有点棘手的解决方案。
或者考虑修改子视图的可见性属性,您可能会得到类似的行为,我认为这会更干净。
编辑:
随着新的 Android 版本,L 开发者预览版,我们似乎终于能够轻松更改视图的 Z 顺序了。 “Elevation”和“TranslationZ”属性可用于救援:https://developer.android .com/preview/material/views-shadows.html
Afaik there's no built-in solution for this. I guess you're trying to modify the z-order in a FrameLayout, or something similar.
However, I think you can modify the order of the contained child elements in the layout. RemoveChild...() and addView() methods can take position values, so you could most likely swap child elements around, and that would modify the z-order. It seems a bit hacky solution however.
Or consider modifying the visibility property of the child views, you may get similar behaviour, and that'd be much cleaner I think.
Edit:
With the new Android version, the L Developer Preview it seems that at last we have the ability to easily change the Z ordering of Views. 'Elevation' and 'TranslationZ' properties to the rescue: https://developer.android.com/preview/material/views-shadows.html
在你想要进入最前面的视图上调用
bringToFront()
方法,然后在所有视图上调用invalidate()
方法,包括你想要在最前面的视图。对所有听众重复同样的操作。因此,当另一个视图的侦听器被调用时,前一个视图将失效并进入后台。
Call
bringToFront()
on the view you want to get in the front, and then call theinvalidate()
method on all the view including the view which you want in the front. Repeat same for all the listeners.So when another view's listener will get called, the previous view will get invalidated and will be in background.
这是我用来将视图发送到后面的方法(与bringToFront相反,类似于sendToBack):
希望这有帮助!
Here is the method I am using to send a View to the back (so opposite of bringToFront, kind of sendToBack):
Hope this helps!
请注意,从 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.它工作得很好,就像bringToBack()
it work's fine like bringToBack()
如果您使用的是 appCompat 库,还有另一种方法可以更改低于 21 API 级别的设备的 Z 顺序:
If you're using appCompat library, there's another way to change Z order for devices lower than 21 API level:
在您的 XML 文件中,您必须按顺序编写:
在这种情况下,
imageView
将位于FloatingActionButton
的后面。如果您想更改某些内容,那么您将以编程方式执行此操作:
按照视图声明的顺序,您将再次从浮动按钮返回图像。所以先声明的视图将从后声明的视图中返回,而实现顺序的关键是
FrameLayout
或的
并在以编程方式添加时再次添加此视图。removeAllViews
LinearLayoutIn your XML File you must to write in order:
In this case, the
imageView
will be back of theFloatingActionButton
.If you want change something, and you will do it programmatically then:
In this order of view declarations, you will have again the image back from the floating button. So the view that is declared first will be back from the view that is declared secondly, and the key to achieve the order, is to
removeAllViews
of aFrameLayout
orLinearLayout
and add again this views when you are adding programmatically.您还可以尝试使用 ViewGroup.addViewInLayout() 方法的功能,该方法采用索引,确定视图应该位于列表的末尾还是开头
You can also try to use functionality of
ViewGroup.addViewInLayout()
method which take an index, determining should the view be at the end or at the beginning of the list如果相关视图是按钮,则编程调整需要单行 xml 来实现完整的 z 顺序控制。否则,setElevation()、setZ()、setTranslationZ() 和bringToFront() 都会莫名其妙地让你失败。请参阅下面有关单独线程的描述。
https://stackoverflow.com/a/68674834/14116101
If the views in question are buttons, programmatic adjustments require a single line of xml for full z-order control. Otherwise, setElevation(), setZ(), setTranslationZ(), and bringToFront() will all inexplicably fail you. See description on separate thread below.
https://stackoverflow.com/a/68674834/14116101
我要么将其可见性设置为 0,要么将其下方的视图置于其前面。
I would either set it's visibility to 0 or just bring the view under it in front of it.
您可以尝试执行
view.setVisibility(View.INVISIBLE)
或view.setVisibility(View.GONE)
。更新:
这向您展示了如何完成你想做的事。
You could try doing
view.setVisibility(View.INVISIBLE)
orview.setVisibility(View.GONE)
.UPDATE:
This shows you how to accomplish what you want to do.