什么是背后R.java概念? [英] What is the concept behind R.java?

查看:157
本文介绍了什么是背后R.java概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的R.java是用来提供访问在XML文件中定义的资源。 要访问,我们需要调用 findViewById()方法传入资源的ID要获取资源。

这是类似于春天在哪里豆在XML上下文中定义,并通过使用应用程序上下文中获取。 context.getBean(将beanId)

本规定自豆外部定义,也没有留下修改到code可以改变的松耦合。

这有我困惑。虽然Android的东西做类似于弹簧,它提供了什么好处呢?

  
      
  1. 在什么反正有一个中间R.java的观点是​​?难道我们只是直接获取资源从XML通过使用资源   读取器/应用程序上下文。例如 findViewById(RESOURCEID)
  2.   
  3. 在没有任何松散的耦合。由于R.java得到引用自动生成一个人如何可以删除资源,并把在一个新的?
  4.   
  5. 在它遵循什么样的设计模式(如果存在的话)?
  6.   
  7. 在那岂不是更好地让资源使用IOC注入(如Roboguice)?那么为什么谷歌决定给我们这样   与资源工作的一个奇怪的方式?
  8.   

请原谅我的无知。我是一个新手的Java开发人员尝试的东西太多了一次。 :-) 感谢所有的反馈。

解决方案

android.R.java 的不仅是那里的 XML 的ID存储。它也包含了对资源的访问 - 如可绘制区域布局,字符串,数组,基本上任何你可以在资源声明

我个人觉得,它使用Eclipse时非常有用。我可以简单地输入 findViewById(R.id。和Eclipse将显示带有选项列表的提示选择。

然而,在一个平台级的,我要说的是硬codeD ID变量使用字符串时查明资源帮助prevent错误 - 这可能是调试的同时,编程(或编译过程中,而不是运行时)。

In android R.java is used to provide access to resources defined in XML files. To access the resource we need to invoke findViewById() method passing in the id of the resource to be fetched.

This is similar to Spring where beans are defined in a XML context and are fetched by using application context. context.getBean("beanId")

This provides loose coupling since the beans are defined externally and could be changed without making modifications to the code.

This has me confused. Though what Android does looks similar to spring, what advantage does it offer?

  1. What is the point of having an intermediate R.java anyway? Couldn't we just acquire resources directly from XML by use of a resource reader/application context. e.g. findViewById("resourceId")
  2. There isn't any loose coupling. Since references in R.java get auto-generated how could one delete a resource and put in a new one?
  3. What design pattern does it follow(if there is one)?
  4. Wouldn't it be better to have resources injected using IOC (like Roboguice)? Why did then google decide to give us such a wierd way of working with resources?

Pardon my ignorance. I'm a newbie Java developer trying too many things at once. :-) Thanks for all the feedback.

解决方案

android.R.java is not just where XML ids are stored. It also contains access to resources - such as drawables, layouts, strings, arrays, and basically anything you can declare in resources.

Personally I find that it is useful when using Eclipse. I can simply type findViewById(R.id. and Eclipse will show a tooltip with a list of options to choose from.

However at a platform level, I would say that the hardcoded id variables help prevent errors when using Strings to identify resources -- something that can be debuggable while programming (or during compilation, rather than runtime).

这篇关于什么是背后R.java概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆