Android 在所有内容上叠加一个视图? [英] Android overlay a view ontop of everything?
问题描述
你可以在 android 中的所有内容之上叠加一个视图吗?
Can you overlay a view on top of everything in android?
在 iPhone 中,我会将新视图的 frame.origin
设置为 (0,0) 并将其宽度和高度设置为 self.view
的宽度和高度.将它添加到 self.view
将导致它充当覆盖层,覆盖后面的内容(或者如果它有透明背景,则显示后面的视图).
In iPhone I would get the new view set its frame.origin
to (0,0) and its width and height to the width and height of self.view
. Adding it to self.view
would then cause it to act as an overlay, covering the content behind (or if it had a transparent background then showing the view behind).
android 中有类似的技术吗?我意识到视图略有不同(有三种类型(或更多......)相对布局、线性布局和框架布局)但有没有办法不加选择地将视图覆盖在所有内容之上?
Is there a similar technique in android? I realise that the views are slightly different (there are three types (or more...) relativelayout, linearlayout and framelayout) but is there any way to just overlay a view on top of everything indiscriminately?
推荐答案
只需使用 RelativeLayout
或 FrameLayout
.最后一个子视图将覆盖其他所有内容.
Simply use RelativeLayout
or FrameLayout
. The last child view will overlay everything else.
Android 支持 Cocoa Touch SDK 不支持的模式:布局管理.
iPhone的布局意味着绝对定位所有内容(除了一些拉伸因素).android 中的布局意味着孩子将被放置在彼此相关的位置.
Android supports a pattern which Cocoa Touch SDK doesn't: Layout management.
Layout for iPhone means to position everything absolute (besides some strech factors). Layout in android means that children will be placed in relation to eachother.
示例(第二个 EditText 将完全覆盖第一个):
Example (second EditText will completely cover the first one):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root_view">
<EditText
android:layout_width="fill_parent"
android:id="@+id/editText1"
android:layout_height="fill_parent">
</EditText>
<EditText
android:layout_width="fill_parent"
android:id="@+id/editText2"
android:layout_height="fill_parent">
<requestFocus></requestFocus>
</EditText>
</FrameLayout>
FrameLayout
是某种视图堆栈.专为特殊情况而设计.
FrameLayout
is some kind of view stack. Made for special cases.
RelativeLayout
非常强大.您可以定义规则,例如视图 A 必须对齐父布局底部,视图 B 必须将 A 底部与顶部对齐,等等
RelativeLayout
is pretty powerful. You can define rules like View A has to align parent layout bottom, View B has to align A bottom to top, etc
根据评论更新
通常您在 onCreate
中使用 setContentView(R.layout.your_layout)
设置内容(它会为您扩充布局).您可以手动执行此操作并调用 setContentView(inflatedView)
,没有区别.
Usually you set the content with setContentView(R.layout.your_layout)
in onCreate
(it will inflate the layout for you). You can do that manually and call setContentView(inflatedView)
, there's no difference.
视图本身可能是单个视图(如 TextView
)或复杂的布局层次结构(嵌套布局,因为所有布局本身都是视图).
The view itself might be a single view (like TextView
) or a complex layout hierarchy (nested layouts, since all layouts are views themselves).
调用 setContentView
后,您的 Activity 知道其内容是什么样的,您可以使用 (FrameLayout) findViewById(R.id.root_view)
检索此层次结构中的任何视图(一般模式 (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view)
).
After calling setContentView
your activity knows what its content looks like and you can use (FrameLayout) findViewById(R.id.root_view)
to retrieve any view int this hierarchy (General pattern (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view)
).
这篇关于Android 在所有内容上叠加一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!