Activity.addContentView(View) == ViewGroup.addContentView(View)?

丶灬走出姿态 提交于 2019-11-27 13:57:56

问题


I have a question regarding Android Activitys:

An Activity has the Method addContentView(View) while a ViewGroup has a (similar?) addView(View) Method.

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?

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?


回答1:


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).

You can check this by using a tool called hierachyviewer from your ANDROID_SDK\tools folder. Here are two screenshots:

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.

After adding a TextView via addContentView() it looks like this. You can see that the base FrameLayout got a new child.



来源:https://stackoverflow.com/questions/9979648/activity-addcontentviewview-viewgroup-addcontentviewview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!