R.layout.activity_main在android开发中的含义(JAVA语言) [英] Meaning of R.layout.activity_main in android development (JAVA language)

查看:158
本文介绍了R.layout.activity_main在android开发中的含义(JAVA语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R.layout.activity_main是什么意思?

What is the meaning of R.layout.activity_main ?

我理解."运算符用于定义特定对象的变量,但是在这种情况下,它被使用了两次,因此我无法从中进行任何操作.另外,"R"和布局"到底是什么?

I understand that "." operator is used to define variables of a particular object but in this case its been used twice so I can't make anything out of it. Also what exactly is "R" and "layout"?

我的意思是显然它们是类(对吗?),但是它们的功能是什么?基本上解释一下R.layout.activity_main!

I mean obviously they are classes (right?) but what is their function ? Basically explain R.layout.activity_main !

如果问题太模糊或太广泛,请发表评论.

Please comment if question too vague or too broad.

推荐答案

R.java是在构建过程中生成的类(具有内部类,例如layoutstring),其中引用了您的应用程序资源.您创建的每个资源(或Android提供的资源)都由R中的整数引用,称为资源ID.

R.java is a class (with inner classes, like layout or string) generated during the build process with references to your app's resources. Every resource you create (or which is provided by Android) is referenced by an integer in R, called a resource id.

R.layout.*引用通常在/res/layout中创建的任何布局资源.因此,如果您创建了一个名为activity_main.xml的活动布局,则可以使用R.layout.activity_main中的引用进行访问.许多内置功能很容易接受这样的资源ID,例如 setContentView(int layoutResid) ,它是您在创建活动时使用的,并且可能在该示例中遇到过.

R.layout.* references any layout resource you have created, usually in /res/layout. So if you created an activity layout called activity_main.xml, you can then use the reference in R.layout.activity_main to access it. Many built-in functionality readily accepts such a resource id, for example setContentView(int layoutResid) which you use during the creation of your activity and where you probably encountered this particular example.

如果您这样创建一个字符串资源(在strings.xml中):

If you create a string resource (in strings.xml) like this:

<string name="app_name">Application name</string>

它将在R.string.app_name中获得新的参考.然后,您可以在任何接受字符串资源的地方使用它,例如,在AndroidManifest.xml或TextView中用于应用程序的android:label.要么在xml中:

it will get a new reference in R.string.app_name. You can then use this everywhere where a string resource is accepted, for example the android:label for your application in AndroidManifest.xml, or on a TextView; either in the xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    />

或代码中的代码:textview.setText(R.string.app_name).

您可以使用 Resources 以编程方式访问资源类,您可以通过调用 getResources 在任何情况下(例如您的活动).因此,例如,您可以通过调用this.getResources().getString(R.string.app_name)来获得活动中上述的应用名称.

You can access resources programmatically using the Resources class, to which you can get a reference by calling getResources on any context (like your activity). So for example you can get your app name described above in your activity by calling this.getResources().getString(R.string.app_name).

您还可以为不同的设备属性/设置(例如屏幕尺寸或语言)提供不同的资源,您可以使用R中的相同引用来访问这些资源.这里最简单的例子是恕我直言,是字符串:如果您在/res中使用语言说明符添加了一个新的values文件夹(对于荷兰语,则为/res/values-nl),并且您添加了具有相同标识符但翻译和资源不同的字符串管理系统会根据用户的设备巧妙地找出要为您提供的服务.

You can also supply different resources for different device properties/settings (like screen size or language), which you can access using the same references in R. The easiest example here, imho, is strings: if you add a new values folder in /res with a language specifier (so /res/values-nl for Dutch) and you add strings with the same identifier but a different translation and the resource management system cleverly figures out which one to provide for you based on your user's device.

我希望这会有所帮助.有关资源的更多信息,请参见文档.

I hope this helps a bit. For more information on resources see the documentation.

这篇关于R.layout.activity_main在android开发中的含义(JAVA语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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