引用字符串的方式

在res/values/strings.xml文件中,有如下代码:

<resources>
	<string name="app_name">HelloWorld</string>
</resources>

这里定义了一个应用程序名的字符串,引用它的方式有两种:

  • 在代码中可以通过R.string.app_name来获得该字符串的引用
  • 在XML中可以通过@string/app_name来获得该字符串的引用

以上的string部分是可以被替换的,引用的什么资源就换成什么。
例如,在AndroidManifest.xml文件中有如下代码:

 <application
               android:allowBackup="true"
               android:icon="@mipmap/ic_launcher"
               android:label="@string/app_name"
               android:supportsRtl="true"
               android:theme="@style/AppTheme">
               …
 </application>

如上所示,HelloWorld项目的应用图标是通过android:icon属性指定的,应用名称则是android:label属性指定的。

发布了25 篇原创文章 · 获赞 18 · 访问量 2130

猜你喜欢

转载自blog.csdn.net/weixin_43568110/article/details/90813580