android:如何使子视图与父视图重叠?
我需要实现如图所示的布局。 Parent 和 Sibling 位于垂直 LinearLayout 中。所以我需要创建一个子视图来重叠它的父视图。我可以在安卓上这样做吗?
I need to implement the layout as in the picture. Parent and Sibling are in a vertical LinearLayout. So I need to make a child view to overlap it's parent. Can I do that in android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用RelativeLayout,那么实现这种效果应该没有问题。默认情况下,如果您不为所有子级提供 android:layout 参数,它将在左上角将其所有子级堆叠在一起。所以它肯定会支持重叠的孩子。您只需要找出告诉它孩子应该在屏幕上相对于其他东西的位置的最佳方式是什么。
If you use a RelativeLayout you should have no problem achieving this effect. By default it will stack all of its children on top of each other in the top left corner if you don't supply them with android:layout parameters. So it will definitely support overlapping children. You'd just have to figure out what the best way to tell it where the child should go on the screen relative to something else.
至少有两种布局可以做到这一点。绝对布局和相对布局。我建议您将视图放在相对布局中,并使用 LayoutParams 添加它们,指定它们从父级顶部和左侧的偏移量:
There are at least two layouts that can do that. AbsoluteLayout and RelativeLayout. I suggest that you put your views in a RelativeLayout and add them with LayoutParams that specify their offset form the top and left of the parent:
如果:
ViewGroup
,那么也许你可以考虑使用 android:clipChildren 在父级上设置为 false。
If:
ViewGroup
then maybe you could consider using android:clipChildren set to false on parent.
就我而言,我必须在父级的父级上将
android:clipChildren
设置为false
。IE
In my case, I have to set
android:clipCildren
to befalse
on the parent of parent.i.e.
我实际上只是在看一个 FrameLayout 的示例,其中 TextView 覆盖在 ImageView 之上。因此,显然有多种方法可以完成它。你的下一个问题可能是哪一个最好......对此我不知道,但这里有一个人可能:
http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part- 1/
I was actually just looking at an example of a FrameLayout that had a TextView overlaid on top of an ImageView. So, there are obviously multiple ways to get it done. Your next question might be which one is best ... to that I have no idea, but here's a guy that might:
http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/
只需将它们全部包含在relativelayout中,并记住绘制顺序是从上到下,因此将最上面的视图放在XML定义的底部。
Just contain them all within a RelativeLayout, and remember the draw order is top to bottom, so put the top most view on the bottom of the XML definition.