在android中是否可以将一个视图放置在另一个视图上?
我们可以将一个小视图放在另一个大视图之上吗?例如,我有一个 VideoView 正在后台播放文件。在此之上,在中间/角落的某个地方,我想放置另一个 ImageView。
但在 Linear/Relative Layout 中,视图只能依次放置或相对放置,不建议使用 AbsoluteLayout。那我该怎么办?
Can we place a small view over another large view? For example, I have a VideoView which is playing a file in the background. Over this, somewhere in the middle/corner, I want to place another ImageView.
But in Linear/Relative Layout, views can be placed only one after another or relative to each other, and AbsoluteLayout is advised against. So what do I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
FrameLayout
是最简单的ViewGroup
,它按照布局 XML 中定义(或以编程方式添加)的顺序堆叠View
;第一个将较低,最后一个将在顶部。以下是两个
View
堆叠并偏移的示例,以更好地说明这一点:< img src="https://i.sstatic.net/KnUgf.png" alt="在此处输入图像描述">
这是带有两个重叠
TextView
框的实际布局 XML 。两个框的偏移是使用 android:layout_gravity 完成的,而 android:gravity 用于将文本本身在每个框内居中。The
FrameLayout
is the simplestViewGroup
and stacks theViews
in the order they're defined in layout XML (or added programmatically); the first will be lower, and the last will be on top.Here is an example where two
Views
are stacked and offset to better illustrate the point:Here is the actual layout XML with the two overlapping
TextView
boxes. The offset of the two boxes is done usingandroid:layout_gravity
whileandroid:gravity
is used for centering the text itself within each box.以防万一,如果您想将视图放置在 ButtonView 之上,请使用它;
android:elevation="7dp"
用于需要放置在按钮顶部的视图。Just in case if you want to place a view on top of a ButtonView then use this;
android:elevation="7dp"
for the view which needs to be placed on top of the button.FrameLayouts
允许您将每个视图堆叠在下面的视图之上。这也可以通过RelativeLayout
来实现。FrameLayouts
let you pile each view on top of the one below. This can also be achieved with aRelativeLayout
.您还可以使用 Google 引入的新布局 ConstraintLayout 来实现此目的。
You can also do it by using ConstraintLayout a new layout introduced by google.
创建相对布局并将视图放入其中,并添加
要位于另一个视图之上的视图
Create Relative Layout and place views inside it and add
for the view whcich is to be over another one
通过将 XML 文件中的后向布局放在前向布局之前,可以轻松完成此操作。
例如,如果我想
cardView1
出现在cardView2
上,我会编写以下代码:由于
cardView2
出现在cardView1
之前,< code>cardView1 是位于另一个之上的一个。It can be done easily by putting the back layouts before forwarded layouts in XML file.
for example if I want to
cardView1
comes overcardView2
I write this code:Since
cardView2
comes beforecardView1
,cardView1
is the one that comes over the other one.解决了完全相同的问题。我通过将所需的元素放置在两个布局之间并使用
android:layout_marginTop
属性的负值来做到这一点。这是代码:
以及它在布局中的外观:
我还不允许发布图片,所以这里是一个链接
Quite the same problem solved. I did it by placing the needed element between two layouts and using negative value for
android:layout_marginTop
attribute.Here's the code:
And how it looks in the layout:
I'm not allowed to post pictures yet, so here's a link to it