Android(安卓)获取根视图
16lz
2021-12-23
代码
View decorView = getWindow().getDecorView();View androidContent = findViewById(android.R.id.content);View androidContentFirstChild = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);View rootView = androidContentFirstChild.getRootView();Log.i(TAG, "decorView: " + decorView.toString());Log.i(TAG, "androidContent: " + androidContent.toString());Log.i(TAG, "androidContent.getParent: " + androidContent.getParent());Log.i(TAG, "androidContentFirstChild: " + androidContentFirstChild.toString());Log.i(TAG, "rootView: " + rootView.toString());Log.i(TAG, "decoView.getChildCount: " + ((ViewGroup)decorView).getChildCount());Log.i(TAG, "androidContent.getChildCount: " + ((ViewGroup)androidContent).getChildCount());
运行截图
结论
- decorView 与 rootView 是一个东西,都是页面的最顶层根布局,它有且仅有一个子视图
android.R.id.content
是一个 FrameLayout,里面才是我们在 layout 里面写的 xml 布局
Android Studio 查看布局结构
Tools ——> Layout Inspector,打开布局检查器对话框
注意,这里只有 debug 版本的 app 才可以查看布局的(第三方APP是无法查看的)
视图树
- 最顶层是 DecorView,其下是一个 LinearLayout,包含了一个惰性布局的 ActionBar 和 FrameLayout 容器
- 自带的 FrameLayout 的 id 固定为
android:id="@android:id/content"
,它的子布局才是我们在 xml 里面写的布局
更多相关文章
- Android文件路径
- Android(安卓)通过handler和message在子线程里面去更新UI
- Eclipse下安装Android(安卓)ADT插件问题
- android 內存等使用情況(转)
- android aapt使用小结
- 在android里面调用javascript代码
- android 模拟器上GPS的使用
- android 圆形的图片里面带字
- android中调用requestFocus()的详细过程