Android视图层次结构的工作原理

时间:2012-10-15 04:49:39

标签: android-2.1-eclair

我有一个WebView,我想扩展到全屏。用户将与其进行交互,因此在扩展时需要保持某种状态。理想的过程是:

- Remove WebView from its parent
- Put WebView at the top of the current View hierarchy with `FILL_PARENT` for width and height

稍后,我需要把WebView放回去:

- Remove WebView from top of the hierarchy
- Restore the old hierarchy
- Put WebView back where it was with its previous setting for width and height

我知道getRootView,但我不知道如何使用它。什么是DecorView

如果有人有一些示例代码来完成我上面描述的扩展和折叠行为,我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:2)

获取DecorView

的根视图
ViewGroup parentViewGroup = (ViewGroup) webView.getRootView().findViewById(android.R.id.content);
View rootview = parentViewGroup.getChildAt(0);

删除该根视图:

parentViewGroup.removeViewAt(0);

从其父级中删除WebView

View webViewParent = (View) webView.getParent();
webViewParent.removeView(webView);

WebView放在视图层次结构的顶部:

parentViewGroup.addView(webView, 0, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));

使用rootview引用撤消扩展。

唯一的问题是:我不知道你将如何恢复rootview的布局参数。