Android View视图------Android如何创建一个view。

  1. }

  2. parseInclude(parser, parent, attrs);

  3. } else if (TAG_MERGE.equals(name)) {

  4. throw new InflateException(“ must be the root element”);

  5. } else {         //看这里,创建view的方法。而且这里已经重新获得了它的

  6. final View view = createViewFromTag(name, attrs);

  7. final ViewGroup viewGroup = (ViewGroup) parent;

  8. final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);

  9. rInflate(parser, view, attrs);

  10. viewGroup.addView(view, params);

  11. }

  12. }

  13. parent.onFinishInflate();

  14. }

  15. View createViewFromTag(String name, AttributeSet attrs) {

  16. if (name.equals(“view”)) {

  17. name = attrs.getAttributeValue(null, “class”);

  18. }

  19. if (DEBUG) System.out.println("******** Creating view: " + name);

  20. try {

  21. View view = (mFactory == null) ? null : mFactory.onCreateView(name,

  22. mContext, attrs);

  23. if (view == null) {

  24. if (-1 == name.indexOf(‘.’)) {       //这里只是为了判断xml文件中tag的属性是否加了包名

  25. view = onCreateView(name, attrs);

  26. } else {

  27. view = createView(name, null, attrs);

  28. }

  29. }

  30. if (DEBUG) System.out.println("Created view is: " + view);

  31. return view;

  32. } catch (InflateException e) {

  33. throw e;

  34. } catch (ClassNotFoundException e) {

  35. InflateException ie = new InflateException(attrs.getPositionDescription()

  36. + ": Error inflating class " + name);

  37. ie.initCause(e);

  38. throw ie;

  39. } catch (Exception e) {

  40. InflateException ie = new InflateException(attrs.getPositionDescription()

  41. + ": Error inflating class " + name);

  42. ie.initCause(e);

  43. throw ie;

  44. }

  45. }

  46. /**

  47. * 真正创建一个view的方法,

  48. * 此方法是用反射获取构造器来实例对象而不是直接new出来这是为了处于性能优化考虑,

  49. * 同一个类名的不同对象,可以直接得到缓存的构造器直接获取一个构造器对象实例。而不需要

  50. * 重复进行new操作。

  51. *

  52. * @param name 此View的全名

  53. * @param prefix 前缀,值为 "android.view."其实就是是否包含包名

  54. * @param attrs 此view的属性值,传递给此view的构造函数

  55. */

  56. public final View createView(String name, String prefix, AttributeSet attrs)

  57. throws ClassNotFoundException, InflateException {

  58. Constructor constructor = sConstructorMap.get(name); //缓存中是否已经有了一个构造函数

  59. Class clazz = null;

  60. try {

  61. if (constructor == null) {

  62. //通过类名获得一个class对象

  63. clazz = mContext.getClassLoader().loadClass(

  64. prefix != null ? (prefix + name) : name);

  65. if (mFilter != null && clazz != null) {

  66. boolean allowed = mFilter.onLoadClass(clazz);

  67. if (!allowed) {

  68. failNotAllowed(name, prefix, attrs);

  69. }

  70. }

  71. //通过参数类型获得一个构造器,参数列表为context,attrs

  72. constructor = clazz.getConstructor(mConstructorSignature);

  73. sConstructorMap.put(name, constructor);      //把此构造器缓存起来

  74. } else {

  75. // If we have a filter, apply it to cached constructor

  76. if (mFilter != null) {

  77. // Have we seen this name before?

  78. Boolean allowedState = mFilterMap.get(name);

  79. if (allowedState == null) {

  80. // New class – remember whether it is allowed

  81. clazz = mContext.getClassLoader().loadClass(

  82. prefix != null ? (prefix + name) : name);

  83. boolean allowed = clazz != null && mFilter.onLoadClass(clazz);

  84. mFilterMap.put(name, allowed);

  85. if (!allowed) {

  86. failNotAllowed(name, prefix, attrs);

  87. }

  88. } else if (allowedState.equals(Boolean.FALSE)) {

  89. failNotAllowed(name, prefix, attrs);

  90. }

  91. }

  92. }

  93. Object[] args = mConstructorArgs;

  94. args[1] = attrs;     //args[0]已经在前面初始好了。这里只要初始化args[1]

  95. return (View) constructor.newInstance(args);     //通过反射new出一个对象。。大功告成

  96. } catch (NoSuchMethodException e) {

  97. InflateException ie = new InflateException(attrs.getPositionDescription()

  98. + ": Error inflating class "

  99. + (prefix != null ? (prefix + name) : name));

  100. ie.initCause(e);

  101. throw ie;

  102. } catch (ClassNotFoundException e) {

  103. // If loadClass fails, we should propagate the exception.

  104. throw e;

  105. } catch (Exception e) {

  106. InflateException ie = new InflateException(attrs.getPositionDescription()

  107. + ": Error inflating class "

  108. + (clazz == null ? “” : clazz.getName()));

  109. ie.initCause(e);

  110. throw ie;

  111. }

  112. }

在类android.content.res.Resources类中获取XmlResourceParser对象;

Java代码   收藏代码

  1. public XmlResourceParser getLayout(int id) throws NotFoundException {

  2. return loadXmlResourceParser(id, “layout”);

  3. }

  4. ackage*/ XmlResourceParser loadXmlResourceParser(int id, String type)

  5. throws NotFoundException {

  6. synchronized (mTmpValue) {

  7. /*TypedValue对象保存了一些有关resource 的数据值,比如说,对于一个view来说,在xml

  8. 文件中可以定义许多属性,TypedValue保存了其中一个属性的相关信息,包括此属性的值的类型

  9. type,是boolean还是color还是reference还是String,这些在attr.xml文件下都有定义。

  10. 它的值的字符串名称;一个属性有多个值时,它从xml文件中获取的值它的顺序data;如果此属性的值

  11. 的类型是一个reference则保存它的resource id的等等。

  12. */

  13. TypedValue value = mTmpValue;

  14. getValue(id, value, true);

  15. if (value.type == TypedValue.TYPE_STRING) {

  16. return loadXmlResourceParser(value.string.toString(), id,

  17. value.assetCookie, type);

  18. }

  19. throw new NotFoundException(

  20. “Resource ID #0x” + Integer.toHexString(id) + " type #0x"

  21. + Integer.toHexString(value.type) + " is not valid");

  22. }

  23. }

  24. /**

  25. *  getValue方法,id表示要查找的控件的 id,outValue是一个对象,用于保存一些属性相关信息

  26. *  resolveRefs为true表明,当通过属性id找到xml文件中的标签时,比如是一个

  27. * 它的值是一个引用reference,则继续解析获得这个id的值。这里看AssetManager类的实现*/

  28. public void getValue(int id, TypedValue outValue, boolean resolveRefs)

  29. throws NotFoundException {

  30. boolean found = mAssets.getResourceValue(id, outValue, resolveRefs);

  31. if (found) {

  32. return;

  33. }

  34. throw new NotFoundException(“Resource ID #0x”

  35. + Integer.toHexString(id));

  36. }

  37. /*package*/ XmlResourceParser loadXmlResourceParser(String file, int id,

  38. int assetCookie, String type) throws NotFoundException {

  39. if (id != 0) {

  40. try {

  41. //取缓存

  42. synchronized (mCachedXmlBlockIds) {

  43. // First see if this block is in our cache.

  44. final int num = mCachedXmlBlockIds.length;

  45. for (int i=0; i<num; i++) {

  46. if (mCachedXmlBlockIds[i] == id) {

  47. //System.out.println(“**** REUSING XML BLOCK!  id=”

  48. //                   + id + “, index=” + i);

  49. return mCachedXmlBlocks[i].newParser();

  50. }

  51. }

  52. //第一次加载时,会打开这个文件获取一个xml数据块对象。

  53. // 这里先看AssetManager类的实现

  54. XmlBlock block = mAssets.openXmlBlockAsset(

  55. assetCookie, file);

  56. //下面会把此xmlBlock对象缓存起来,保存id和block,

  57. //以后如果是同样的id,直接在缓存中取XmlBlock。

  58. //这样就不用再在本地方法中打开文件创建解析树了。

  59. if (block != null) {

  60. int pos = mLastCachedXmlBlockIndex+1;

  61. if (pos >= num) pos = 0;

  62. mLastCachedXmlBlockIndex = pos;

  63. XmlBlock oldBlock = mCachedXmlBlocks[pos];

  64. if (oldBlock != null) {

  65. oldBlock.close();

  66. }

  67. mCachedXmlBlockIds[pos] = id;

  68. mCachedXmlBlocks[pos] = block;

  69. //返回的内部类继承了XmlResourceParser,在APi中此类是隐藏的

  70. return block.newParser();

  71. }

  72. }

  73. } catch (Exception e) {

  74. NotFoundException rnf = new NotFoundException(

  75. “File " + file + " from xml type " + type + " resource ID #0x”

  76. + Integer.toHexString(id));

  77. rnf.initCause(e);

  78. throw rnf;

  79. }

  80. }

  81. throw new NotFoundException(

  82. “File " + file + " from xml type " + type + " resource ID #0x”

  83. + Integer.toHexString(id));

  84. }

android.content.res.AssetManager类

Java代码   收藏代码

  1. /*package*/ final boolean getResourceValue(int ident,

  2. TypedValue outValue,

  3. boolean resolveRefs)

  4. {

  5. int block = loadResourceValue(ident, outValue, resolveRefs);

  6. if (block >= 0) {

  7. if (outValue.type != TypedValue.TYPE_STRING) {

  8. return true;

  9. }

  10. //mStringBlocks通过本地方法保存所有布局文件的文件名

  11. outValue.string = mStringBlocks[block].get(outValue.data);

  12. return true;

  13. }

  14. return false;

  15. }

  16. //这是一个本地方法,是在本地方法中获取这个控件信息,返回通过此控件的id找到的文件名

  17. //的位置,由于个人对c++不是很了解,只初略的解释本地方法的一些功能。

  18. //对于的JNI文件位于:\frameworks\base\core\jni\android_util_AssetManager.cpp

  19. private native final int loadResourceValue(int ident, TypedValue outValue,

  20. boolean resolve);

  21. /**

  22. * 通过文件名,在本地方法中找到这个xml文件,并且在本地方法中生成一个xml解析对象。

  23. * 返回一个id,这个id对应java中的xmlBlock对象。这样xml文件就被load进了内存。

  24. * 也就是android所说的预编译,以后再访问只要直接去取数据即可

  25. */

  26. /*package*/ final XmlBlock openXmlBlockAsset(int cookie, String fileName)

  27. throws IOException {

  28. synchronized (this) {

  29. if (!mOpen) {

  30. throw new RuntimeException(“Assetmanager has been closed”);

  31. }

  32. int xmlBlock = openXmlAssetNative(cookie, fileName);

  33. if (xmlBlock != 0) {

  34. /*

  35. * 在XmlBlock对象中,终于到找了实现XmlResourceParser接口的类

  36. * Parser,它是XmlBlock的一个内部类。这里面可以获取所有xml文件中的内容。

  37. * 不管是属性还是Tag标签。这里xmlBlock是用来与本地类中的解析树对象对应的。

  38. * 所有的解析方法,其实都是调用的本地xml解析树中的方法。所以此类中有大量的

  39. * 本地方法。

  40. */

  41. XmlBlock res = new XmlBlock(this, xmlBlock);

  42. incRefsLocked(res.hashCode());

  43. return res;

  44. }

  45. }

  46. throw new FileNotFoundException("Asset XML file: " + fileName);

  47. }

三 。通过view.findViewById(resourceid)获得一个view的实例

android.View.View类中

Java代码   收藏代码

  1. //调用了通过id检索view的方法

  2. public final View findViewById(int id) {

  3. if (id < 0) {

  4. return null;

  5. }

  6. return findViewTraversal(id);

  7. }

  8. //不是吧,这不是坑爹吗?猜想肯定是被viewgroup重写了

  9. protected View findViewTraversal(int id) {

  10. if (id == mID) {

  11. return this;

  12. }

  13. return null;

  14. }

android.View.ViewGroup类中

Java代码   收藏代码

  1. //哈哈,果然重写了此方法。其实就是在viewgroup包含的

  2. //子view数组中进行遍历。那么view是什么时候被加入进

  3. //viewgroup中的呢?如果是在代码中写,肯定是直接使用

  4. //addView方法把view加入viewGroup。如果写在xml布局文件

  5. //中,其实是在第二种方法中被加入view的。inflate加载父view

  6. //时会同时把其所有的子view加载完,同时addView到父view中

  7. protected View findViewTraversal(int id) {

  8. if (id == mID) {

  9. return this;

  10. }

  11. final View[] where = mChildren;

  12. final int len = mChildrenCount;

  13. for (int i = 0; i < len; i++) {

  14. View v = where[i];

  15. if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {

  16. v = v.findViewById(id);

  17. if (v != null) {

  18. return v;

  19. }

  20. }

  21. }

  22. return null;

  23. }

四。通过activity的setContentView方法和findViewById获取一个view的实例。

它是通过

getWindow().setContentView(layoutResID);设置window对象的view

再来看看window对象是在哪里获得到的,在类Activity中找到

mWindow = PolicyManager.makeNewWindow(this);

它是由PolicyManager生成的。

找到com.android.internal.policy.PolicyManager,找到方法

Java代码   收藏代码

  1. //window是由sPolicy对象创建的

  2. public static Window makeNewWindow(Context context) {

  3. return sPolicy.makeNewWindow(context);

  4. }

  5. //sPolicy对象是通过反射,获取的一个实例

  6. //此类的实现在com.android.internal.policy.impl.Policy中

  7. private static final String POLICY_IMPL_CLASS_NAME =

  8. “com.android.internal.policy.impl.Policy”;

  9. private static final IPolicy sPolicy;

  10. static {

  11. // Pull in the actual implementation of the policy at run-time

  12. try {

  13. Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);

  14. sPolicy = (IPolicy)policyClass.newInstance();

  15. } catch (ClassNotFoundException ex) {

  16. throw new RuntimeException(

  17. POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);

  18. } catch (InstantiationException ex) {

  19. throw new RuntimeException(

  20. POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);

  21. } catch (IllegalAccessException ex) {

  22. throw new RuntimeException(

  23. POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);

  24. }

  25. }

找到com.android.internal.policy.impl.Policy类

Java代码   收藏代码

  1. public PhoneWindow makeNewWindow(Context context) {

  2. return new PhoneWindow(context);

  3. }

它其实是一个phoneWindow对象,继承自window对象

找到com.android.internal.policy.impl.phoneWindow 看它内部是如何把resourceid加载成一个view的

Java代码   收藏代码

  1. private ViewGroup mContentParent;

  2. //这是window的顶层视图,它包含一些窗口的装饰,比图title bar,状态栏等等

  3. private DecorView mDecor;

  4. //这里的layoutResID也是由mLayoutInflater进行加载的,加载的方式与第二种方法一样。

  5. //只不过这里把的到的view变成了mContentParent的子view

  6. @Override

  7. public void setContentView(int layoutResID) {

  8. if (mContentParent == null) {

  9. installDecor();

  10. } else {

  11. mContentParent.removeAllViews();

  12. }

  13. mLayoutInflater.inflate(layoutResID, mContentParent);

  14. final Callback cb = getCallback();

  15. if (cb != null) {        //这是回调方法,表明mContentParent的子view已经发生改变

  16. cb.onContentChanged();

  17. }

  18. }

  19. //再来看看mContentParent究竟是何物,它肯定是一个viewGroup

  20. private void installDecor() {

  21. if (mDecor == null) {

  22. mDecor = generateDecor();

  23. mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

  24. mDecor.setIsRootNamespace(true);

  25. }

  26. if (mContentParent == null) {

  27. mContentParent = generateLayout(mDecor);

  28. mTitleView = (TextView)findViewById(com.android.internal.R.id.title);

  29. if (mTitleView != null) {        //这里设置的是是否隐藏titleContainer,即头部titlebar

  30. if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {

  31. View titleContainer = findViewById(com.android.internal.R.id.title_container);

  32. if (titleContainer != null) {

  33. titleContainer.setVisibility(View.GONE);

  34. } else {

  35. mTitleView.setVisibility(View.GONE);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

《960全网最全Android开发笔记》

《379页Android开发面试宝典》

《507页Android开发相关源码解析》

因为文件太多,全部展示会影响篇幅,暂时就先列举这些部分截图

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

4年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

[外链图片转存中…(img-pF1vmeC6-1713094985636)]

[外链图片转存中…(img-E7gtaHRB-1713094985636)]

[外链图片转存中…(img-cecC0yUx-1713094985636)]

[外链图片转存中…(img-0Ne5Owzy-1713094985637)]

[外链图片转存中…(img-ATNiJdI1-1713094985637)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

《960全网最全Android开发笔记》

[外链图片转存中…(img-0ECfNPZb-1713094985637)]

《379页Android开发面试宝典》

[外链图片转存中…(img-CARJXqSP-1713094985637)]

《507页Android开发相关源码解析》

[外链图片转存中…(img-9MPczDYr-1713094985638)]

因为文件太多,全部展示会影响篇幅,暂时就先列举这些部分截图

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 17
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值