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

一。在代码中直接new出来。

比如说你要创建一个TextView的实例,那么你可以这样写:

Java代码   收藏代码

  1. TextView text = new TextView©;  //c为context对象,表明textview是在此对象中运行的。

**

二。把控件写在xml文件中然后通过LayoutInflater初始化一个view。**

注意:下面的内容不是顺序的看的而是交替的看的。否则可能弄迷糊。

可以通过

Java代码   收藏代码

  1. //通过系统提供的实例获得一个LayoutInflater对象

  2. LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  3. //第一个参数为xml文件中view的id,第二个参数为此view的父组件,可以为null,android会自动寻找它是否拥有父组件

  4. View view = inflater.inflate(R.layout.resourceid, null);

这样也得到了一个view的实例,让我们一步一步来看,这个view是怎么new出来的。

看类android.view.LayoutInflater

Java代码   收藏代码

  1. public View inflate(int resource, ViewGroup root) {

  2. return inflate(resource, root, root != null);

  3. }

  4. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {

  5. /*可以看到通过resource id返回了一个XmlResourceParser,通过类名就可以猜测

  6. 这是一个xml的解析类。但点进去一看,发现它只是一个接口,它继承自 XmlPullParser用于pull方式解析xml的接口。和AttributeSet用于获取此view的所有属性。

  7. 那么需要能找到它的实现类。先看下面resource类。

  8. */

  9. XmlResourceParser parser = getContext().getResources().getLayout(resource);

  10. try {

  11. return inflate(parser, root, attachToRoot);

  12. } finally {

  13. parser.close();

  14. }

  15. }

  16. /**

  17. * 终于到了重点,获取一个这个View的实例

  18. */

  19. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {

  20. synchronized (mConstructorArgs) {

  21. /**

  22. * 获取一个实现此AttributeSet的实例。因为此XmlPullParser是继承自AttributeSet

  23. * 的,所以parser对象可以直接作为一个AttributeSet对象。也可以用组合的方式

  24. * 把parser传递给另外一个实现自AttributeSet的对象,来获取一个AttributeSet实例。

  25. **/

  26. final AttributeSet attrs = Xml.asAttributeSet(parser);

  27. mConstructorArgs[0] = mContext;      //构造函数的参数,第一个值是此view运行所在的对象context

  28. View result = root;

  29. try {

  30. // parser同时也继承了xmlPullParser,所以可以用pull解析来获取此view的根节点

  31. int type;

  32. while ((type = parser.next()) != XmlPullParser.START_TAG &&

  33. type != XmlPullParser.END_DOCUMENT) {

  34. // Empty

  35. }

  36. if (type != XmlPullParser.START_TAG) {

  37. throw new InflateException(parser.getPositionDescription()

  38. + “: No start tag found!”);

  39. }

  40. //获得根节点标签的名字

  41. final String name = parser.getName();

  42. //如果它的根节点是一个merge对象,则必须手动设置此view的父节点,否则抛出异常

  43. //因为由merge创建的xml文件,常常被其他layout所包含

  44. if (TAG_MERGE.equals(name)) {

  45. if (root == null || !attachToRoot) {

  46. throw new InflateException(" can be used only with a valid "

  47. + “ViewGroup root and attachToRoot=true”);

  48. }

  49. rInflate(parser, root, attrs);

  50. } else {

  51. // 此inflate的xml文件中的root view。即我们通过inflate返回得到的view

  52. View temp = createViewFromTag(name, attrs);

  53. ViewGroup.LayoutParams params = null;

  54. if (root != null) {

  55. // Create layout params that match root, if supplied

  56. params = root.generateLayoutParams(attrs);

  57. if (!attachToRoot) {

  58. // Set the layout params for temp if we are not

  59. // attaching. (If we are, we use addView, below)

  60. temp.setLayoutParams(params);

  61. }

  62. }

  63. // 加载temp下所有的子view

  64. rInflate(parser, temp, attrs);

  65. //如果给出了root,则把此view添加到root中去

  66. if (root != null && attachToRoot) {

  67. root.addView(temp, params);

  68. }

  69. // Decide whether to return the root that was passed in or the

  70. // top view found in xml.

  71. if (root == null || !attachToRoot) {

  72. result = temp;

  73. }

  74. }

  75. } catch (XmlPullParserException e) {

  76. InflateException ex = new InflateException(e.getMessage());

  77. ex.initCause(e);

  78. throw ex;

  79. } catch (IOException e) {

  80. InflateException ex = new InflateException(

  81. parser.getPositionDescription()

  82. + ": " + e.getMessage());

  83. ex.initCause(e);

  84. throw ex;

  85. }

  86. return result;

  87. }

  88. }

  89. /**

  90. *

  91. * 有上至下递归的初始化所有子view和子view的子view。在此方法被调用完成后

  92. * 会调用此view的parent view的onFinishInflate方法。表明其子view全部加载完毕

  93. */

  94. private void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs)

  95. throws XmlPullParserException, IOException {

  96. final int depth = parser.getDepth();

  97. int type;

  98. while (((type = parser.next()) != XmlPullParser.END_TAG ||

  99. parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {

  100. if (type != XmlPullParser.START_TAG) {

  101. continue;

  102. }

  103. final String name = parser.getName();

  104. if (TAG_REQUEST_FOCUS.equals(name)) {

  105. parseRequestFocus(parser, parent);

  106. } else if (TAG_INCLUDE.equals(name)) {

  107. if (parser.getDepth() == 0) {

  108. throw new InflateException(“ cannot be the root element”);

  109. }

  110. parseInclude(parser, parent, attrs);

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

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

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

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

  115. final ViewGroup viewGroup = (ViewGroup) parent;

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

  117. rInflate(parser, view, attrs);

  118. viewGroup.addView(view, params);

  119. }

  120. }

  121. parent.onFinishInflate();

  122. }

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

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

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

  126. }

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

  128. try {

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

  130. mContext, attrs);

  131. if (view == null) {

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

  133. view = onCreateView(name, attrs);

  134. } else {

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

  136. }

  137. }

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

  139. return view;

  140. } catch (InflateException e) {

  141. throw e;

  142. } catch (ClassNotFoundException e) {

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

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

  145. ie.initCause(e);

  146. throw ie;

  147. } catch (Exception e) {

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

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

  150. ie.initCause(e);

  151. throw ie;

  152. }

  153. }

  154. /**

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

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

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

  158. * 重复进行new操作。

  159. *

  160. * @param name 此View的全名

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

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

  163. */

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

  165. throws ClassNotFoundException, InflateException {

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

  167. Class clazz = null;

  168. try {

  169. if (constructor == null) {

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

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

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

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

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

  175. if (!allowed) {

  176. failNotAllowed(name, prefix, attrs);

  177. }

  178. }

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

  180. constructor = clazz.getConstructor(mConstructorSignature);

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

  182. } else {

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

  184. if (mFilter != null) {

  185. // Have we seen this name before?

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

  187. if (allowedState == null) {

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

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

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

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

  192. mFilterMap.put(name, allowed);

  193. if (!allowed) {

  194. failNotAllowed(name, prefix, attrs);

  195. }

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

  197. failNotAllowed(name, prefix, attrs);

  198. }

  199. }

  200. }

  201. Object[] args = mConstructorArgs;

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

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

  204. } catch (NoSuchMethodException e) {

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

  206. + ": Error inflating class "

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

  208. ie.initCause(e);

  209. throw ie;

  210. } catch (ClassNotFoundException e) {

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

  212. throw e;

  213. } catch (Exception e) {

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

  215. + ": Error inflating class "

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

  217. ie.initCause(e);

  218. throw ie;

  219. }

  220. }

在类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. XmlBlock res = new XmlBlock(this, xmlBlock);

  13. incRefsLocked(res.hashCode());

  14. return res;

  15. }

  16. }

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

  18. }

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

android.View.View类中

Java代码   [外链图片转存中…(img-ouJWTNpP-1716487355621)]

  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;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值