55 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: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>
Run Code Online (Sandbox Code Playgroud)
并且,我注意到:
1)
三角警告
下的黄线下面的黄线表示
并且其余的警告也是如此.任何建议?android:text="Yellow"android:text="row four"[I18N] Hardcoded string "Yellow", should use @string resource "
Kuf*_*ffs 121
将字符串硬编码到布局文件中并不是一种好习惯.您应该将它们添加到字符串资源文件中,然后从布局中引用它们.
这样,您只需编辑strings.xml文件,即可同时更新所有布局中每个"黄色"字样.
它对于支持多种语言也非常有用,因为单独的strings.xml文件可用于每种受支持的语言.
示例:保存在res/values/strings.xml的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
此布局XML将字符串应用于视图:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow" />
Run Code Online (Sandbox Code Playgroud)
类似地,颜色应存储在colors.xml中,然后使用@ color/color_name引用
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
小智 13
您必须在strings.xml下创建它们
<string name="close">Close</string>
Run Code Online (Sandbox Code Playgroud)
你必须像这样替换和引用
android:text="@string/close"/>
Run Code Online (Sandbox Code Playgroud)
即使XML文件显示strings.xml,也不要使用@strings,否则它将无效.
Xar*_*mer 10
将字符串硬编码到布局文件/代码中并不是一种好习惯.您应该将它们添加到字符串资源文件中,然后从布局中引用它们.
strings.xml文件即可同时更新所有布局中同一个单词的每个匹配项.supporting multiple languages因为strings.xml file可以将单独的语言用于每种受支持的语言@string系统的实际点请阅读
本地化文档.它允许您轻松地在应用程序中查找文本,然后将其翻译.support multiple languages with a single application package file
(APK).优点