R.layout.activity_main是什么意思?
我明白那个 "." 运算符用于定义特定对象的变量,但在这种情况下,它已被使用了两次,因此我无法从中获取任何内容.究竟什么是"R"和"布局"?
我的意思是他们显然是班级(对吗?)但他们的功能是什么?基本上解释R.layout.activity_main!
如果问题太模糊或太宽,请发表评论.
小智 14
R.java是在构建过程中生成的类(具有内部类,如layout或string),引用了应用程序的资源.您创建的每个资源(或由Android提供的资源)都由一个整数in引用R,称为资源ID.
R.layout.*引用您创建的任何布局资源,通常在/res/layout.因此,如果您创建了一个名为的活动布局activity_main.xml,则可以使用该引用R.layout.activity_main来访问它.许多内置功能很容易接受这样的资源ID,例如setContentView(int layoutResid)您在创建活动期间使用的资源ID 以及您可能遇到此特定示例的位置.
如果您创建一个字符串资源(在strings.xml中),如下所示:
<string name="app_name">Application name</string>
Run Code Online (Sandbox Code Playgroud)
它会得到一个新的参考R.string.app_name.然后,您可以在接受字符串资源的任何地方使用它,例如,android:label在您的应用程序中AndroidManifest.xml,或在TextView上; 要么在xml中:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
/>
Run Code Online (Sandbox Code Playgroud)
或者在代码中:textview.setText(R.string.app_name).
您可以使用Resources类以编程方式访问资源,您可以通过调用getResources任何上下文(如您的活动)来获取引用.例如,您可以通过调用获取活动中的上述应用名称this.getResources().getString(R.string.app_name).
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.
| 归档时间: |
|
| 查看次数: |
13473 次 |
| 最近记录: |