android – 究竟布局通胀发生在哪里?
作者:互联网
嗨,这些问题似乎非常基本,但我没有找到任何答案.
当我们setContentView(R.layout.somelayout)时,我们的布局XML文件被LayoutInflater服务填充.但我想知道它究竟发生在哪里.我没有在活动源代码中找到任何膨胀xml文件的代码.
还有哪些是为R中的视图生成的ID?
提前致谢.
解决方法:
较少的文档和为基本过程编写和调用setContentView()和其他方法的调用.
实际上,当你调用setContentView(R.layout.< layout_file_name>);. Android简介会出现在图片中,它将从其id中为给定的布局文件充气,并为当前的Activity准备View并放入顶级.
调用setContentView(R.layout.< layout_name>)时会发生什么?
Android系统接收此引用并使用它来使树无效,测量和绘制.层次结构的根节点请求其子节点自己绘制 – 在
转而,每个视图组节点负责调用每个自己的子视图来绘制自己.孩子可以在父母中请求大小和位置,但父对象最终决定每个孩子的大小. Android按顺序(从层次结构树的顶部)解析布局元素,实例化视图并将它们添加到父级.因为这些按顺序绘制,如果有重叠位置的元素,
要绘制的最后一个将位于先前被吸引到该空间的其他人之上.
更新:
好的,看了我发现后,
当你调用Activity类基类的setContentView()时,它调用Window类的setContentView()的抽象方法,这是抽象方法,并且该方法的唯一一个覆盖在PhoneWindow类中.看起来像,
@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mLayoutInflater.inflate(layoutResID, mContentParent);
final Callback cb = getCallback();
if (cb != null) {
cb.onContentChanged();
}
}
因此,查看通货膨胀是在这一类中发生的.
更新:2关于R.java文件生成..
Aapt(Android资产包装工具) – 此工具编译所有XML布局文件和AndroidManifest.xml文件.除了编译版本,这还会生成一个R.java文件,其中包含已编译资源的所有引用.
存储在res子目录中的资源包括诸如图标,布局和字符串之类的东西.这些是使用aapt工具编译到名为R.java的文件中,存储在gen /子目录中.
从aapt帮助
aapt p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \
[-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \
[--min-sdk-version VAL] [--target-sdk-version VAL] \
[--max-sdk-version VAL] [--app-version VAL] \
[--app-version-name TEXT] [--custom-package VAL] \
[-I base-package [-I base-package ...]] \
[-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \
[-S resource-sources [-S resource-sources ...]] [-F apk-file] [-J R-file-dir] \
[raw-files-dir [raw-files-dir] ...]
Package the android resources. It will read assets and resources that are
supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R
options control which files are output.
命令为aapt :(对于HelloWorld Android应用程序)
aapt \package -v -f -m -M AndroidManifest.xml -S .\bin\res -S .\res \
-I C:\local\android-sdk-windows\platforms\android-15\android.jar \
-J .\gen --generate-dependencies
结果看起来像,
Configurations:
(default)
hdpi
ldpi
mdpi
Files:
drawable\ic_launcher.png
Src: (hdpi) .\res\drawable-hdpi\ic_launcher.png
Src: (ldpi) .\res\drawable-ldpi\ic_launcher.png
Src: (mdpi) .\res\drawable-mdpi\ic_launcher.png
layout\main.xml
Src: () .\res\layout\main.xml
values\strings.xml
Src: () .\res\values\strings.xml
AndroidManifest.xml
Src: () AndroidManifest.xml
Resource Dirs:
Type drawable
drawable\ic_launcher.png
Src: (hdpi) .\res\drawable-hdpi\ic_launcher.png
Src: (ldpi) .\res\drawable-ldpi\ic_launcher.png
Src: (mdpi) .\res\drawable-mdpi\ic_launcher.png
Type layout
layout\main.xml
Src: () .\res\layout\main.xml
Type values
values\strings.xml
Src: () .\res\values\strings.xml
Including resources from package: \
C:\local\android-sdk-windows\platforms\android-15\android.jar
applyFileOverlay for drawable
trying overlaySet Key=ic_launcher.png
baseFile 0 has flavor ,,,,,,,,,,,hdpi,,,,,,,
baseFile 1 has flavor ,,,,,,,,,,,ldpi,,,,,,,
baseFile 2 has flavor ,,,,,,,,,,,mdpi,,,,,,,
overlayFile 0 has flavor ,,,,,,,,,,,hdpi,,,,,,,
overlayFile 1 has flavor ,,,,,,,,,,,ldpi,,,,,,,
overlayFile 2 has flavor ,,,,,,,,,,,mdpi,,,,,,,
found a match (0) for overlay file ic_launcher.png, \
for flavor ,,,,,,,,,,,hdpi,,,,,,,
found a match (1) for overlay file ic_launcher.png, \
for flavor ,,,,,,,,,,,ldpi,,,,,,,
found a match (2) for overlay file ic_launcher.png, \
for flavor ,,,,,,,,,,,mdpi,,,,,,,
applyFileOverlay for layout
applyFileOverlay for anim
applyFileOverlay for animator
applyFileOverlay for interpolator
applyFileOverlay for xml
applyFileOverlay for raw
applyFileOverlay for color
applyFileOverlay for menu
applyFileOverlay for mipmap
(new resource id ic_launcher from \
.\bin\res\drawable-hdpi\ic_launcher.png)
(new resource id ic_launcher from \
.\bin\res\drawable-ldpi\ic_launcher.png)
(new resource id ic_launcher from \
.\bin\res\drawable-mdpi\ic_launcher.png)
(new resource id main from .\res\layout\main.xml)
Writing symbols for class R.
标签:android,layout-inflater,android-layout 来源: https://codeday.me/bug/20190901/1781671.html