今天尝试国际化安卓应用,配置了中国和美国的字符串资源文件,在布局文件使用了它们,
位置:res/values-en-rUS/strings.xml
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ExtraTranslation">
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="msg">Hello, Android!</string>
</resources>
位置:res/values-zh-rCN/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ExtraTranslation">
<string name="ok">确定</string>
<string name="cancel">取消</string>
<string name="msg">你好啊,安卓</string>
</resources>
位置:res/layout/nationalization.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/na_show"
android:text="@string/msg"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="@string/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="@string/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
资源文件存在且在布局文件中使用了,在编译工程时出现以下错误:
Android resource linking failed
warn: removing resource com.example.studyandroidchapter6_002:string/cancel without required default value.
warn: removing resource com.example.studyandroidchapter6_002:string/msg without required default value.
warn: removing resource com.example.studyandroidchapter6_002:string/ok without required default value.
D:\Android\Project\StudyAndroidChapter6_002\app\src\main\res\layout\nationalization.xml:8: error: resource string/msg (aka com.example.studyandroidchapter6_002:string/msg) not found.
D:\Android\Project\StudyAndroidChapter6_002\app\src\main\res\layout\nationalization.xml:20: error: resource string/ok (aka com.example.studyandroidchapter6_002:string/ok) not found.
D:\Android\Project\StudyAndroidChapter6_002\app\src\main\res\layout\nationalization.xml:24: error: resource string/cancel (aka com.example.studyandroidchapter6_002:string/cancel) not found.
error: failed linking file resources.
解决办法:
右键res/values-en-rUS/strings.xml,点击open translations Editor,填写 默认值,就可以了
上述问题的原因是因为没有配置默认选项,可以直接在values/strings.xml(默认)中添加;添加图片资源也是一样,添加了中美两种资源后还要添加默认值,默认目录为drawable目录,资源名字要和两个资源一样: