问题描述
我有一个关于 Android Activity 的问题:
I have a question regarding Android Activitys:
一个 Activity 有一个方法 addContentView(View)
而一个 ViewGroup 有一个(类似的?)addView(View)
方法.
An Activity has the Method addContentView(View)
while a ViewGroup has a (similar?) addView(View)
Method.
不幸的是,它没有记录来自 addContentView
的视图的放置位置.它是像一个 LinearLayout
只是将视图添加到底部,还是更像一个 FrameLayout
,将其视图添加到onTop"?是否依赖于setContentView
设置的ViewGroup
?
Unfortunately its undocumented where the View from addContentView
is placed. Is it like a LinearLayout
just adding the View to the bottom, or is it more like a FrameLayout
, which adds its Views "onTop" ? Does it depend on the ViewGroup
set by setContentView
?
如果我深入研究来源,我会看到addContentView
将调用Window
的抽象方法addContentView
.不幸的是,我看不到哪个类正在实现这个方法.那么 Activity addContentView
的行为究竟是什么?
If I dive into the sources I see that addContentView
will call Window
's abstract Method addContentView
. Unfortunately I cannot see which class is implementing this Method. So whats the behaviour of Activitys addContentView
exactly?
推荐答案
每个活动的基本布局都是一个 FrameLayout
.这意味着您通常通过 setContentView()
设置的布局是此布局的子项.addContentView()
只添加了另一个孩子,因此它的行为类似于 FrameLayout
(这意味着它会在现有元素之上添加新的 UI 元素).
The base layout of every activity is a FrameLayout
. This means the layout you usually set via setContentView()
is a child of this layout. addContentView()
adds just another child, therefore it behaves like a FrameLayout
(which means it adds new UI elements above existing ones).
您可以使用名为 hierachyviewer
来自您的 ANDROID_SDKools
文件夹.下面是两张截图:
You can check this by using a tool called hierachyviewer
from your ANDROID_SDKools
folder. Here are two screenshots:
这是调用 addContentView()
之前的布局,我的 Activity 由默认的 FrameLayout 组成,其中包含一个带有 Button 的 LinearLayout(我的布局在这里).这反映在这里的底行,上面的其他元素是标题/状态栏.
This is the layout before calling addContentView()
, my activity consists of the default FrameLayout, holding a LinearLayout with a Button (my layout here). This is reflected in the bottom row here, the other elements above are the title/statusbar.
通过 addContentView()
添加 TextView 后,它看起来像这样.你可以看到基础 FrameLayout 有了一个新的孩子.
After adding a TextView via addContentView()
it looks like this. You can see that the base FrameLayout got a new child.
这篇关于Activity.addContentView(View) == ViewGroup.addContentView(View)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!