Android 硬编码字符串“;第三行“;,应该使用@string资源
我是一名android初学者,我试图在eclipse中运行这个线性布局:Android 硬编码字符串“;第三行“;,应该使用@string资源,android,exception,internationalization,Android,Exception,Internationalization,我是一名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:la
<?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=“第四行”
三角形警告显示
[I18N]硬编码字符串“黄色”,应使用@string resource“
其他警告也一样。有什么建议吗?将字符串硬编码到布局文件中不是好做法。您应该将它们添加到字符串资源文件中,然后从布局中引用它们 这允许您通过编辑strings.xml文件,同时更新所有版面中出现的每个单词“Yellow” 它对于支持多种语言也非常有用,因为一个单独的strings.xml文件可以用于每种支持的语言 例如: 保存在res/values/strings.XML上的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
黄色的
此布局XML将字符串应用于视图:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow" />
类似地,颜色应该存储在colors.xml中,然后使用@color/color\u name引用
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
</resources>
#000000
您必须在
strings.xml
<string name="close">Close</string>
即使XML文件显示strings.XML,也不要使用@strings,否则它将无法工作
将字符串硬编码到布局文件/代码中不是一种好做法。您应该将它们添加到字符串资源文件中,然后从布局中引用它们
只需编辑
strings.xml
文件即可同时进行布局strings.xml文件
@string
系统的实际意义请仔细阅读
文档。它允许您轻松地在中查找文本
您的应用程序,然后将其翻译- 假设您在代码中的10个不同位置使用了相同的字符串。 如果你决定改变它怎么办?而不是寻找它的位置 已在项目中使用,您只需更改一次,更改将被删除 反映在项目的各个方面
- 字符串不会使应用程序代码杂乱无章,使其清晰明了 易于维护
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
例如:
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
黄色的
和内部布局:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow" />
(超出范围)另外,它看起来像一个表。可能是一个表布局更合适。此外,不鼓励使用pt中的textsize。sp更灵活。为什么它是@string/…而不是@strings/…呢?你引用的是单个字符串。为什么它是复数?@Kuffs:如果应用程序是单语的,那么小祝酒词就像““请输入小于100的值”需要显示,我们是否仍应遵循字符串资源方法?如果您将来决定添加一种新语言,我会这样做,因为它会使代码更干净,并且您的所有字符串都在一个位置。不过,在一天结束时,这不是一个规则。@crios Great English