问题描述
我是一名初学者 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 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!