hardcoded string quot;row threequot;, should use @string resource(硬编码字符串“第三行,应使用@string 资源)
问题描述
我是一名初学者 android 开发人员,我试图在 eclipse 中运行这个线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
而且,我注意到了:
1) android:text="Yellow"
下的黄线2) android:text="row 四"
下的黄线三角警告说 [I18N] Hardcoded string "Yellow", should use @string resource "
其余的警告也一样.有什么建议吗?
And, I noticed :
1) yellow line under android:text="Yellow"
2) yellow line under android:text="row four"
the Triangle warn says [I18N] Hardcoded string "Yellow", should use @string resource "
and same for the rest of the warnings.Any suggestion?
推荐答案
将字符串硬编码到布局文件中并不是一个好习惯.您应该将它们添加到字符串资源文件中,然后从您的布局中引用它们.
It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.
这允许您通过编辑您的 strings.xml 文件同时更新所有布局中每个出现的单词Yellow".
This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings.xml file.
它对于支持多种语言也非常有用,因为单独的 strings.xml 文件可用于每种支持的语言.
It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language.
示例:保存在 res/values/strings.xml 的 XML 文件:
example: XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
此布局 XML 将字符串应用于视图:
This layout XML applies a string to a View:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow" />
类似的颜色应该存储在 colors.xml 中,然后使用 @color/color_name 引用
Similarly colors should be stored in colors.xml and then referenced by using @color/color_name
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
</resources>
这篇关于硬编码字符串“第三行",应使用@string 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:硬编码字符串“第三行",应使用@string 资源


基础教程推荐
- 如何使 UINavigationBar 背景透明? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01