Android开发第3-1课:支持不同的语言

本文指导您如何在Android项目中创建本地目录和字符串文件,通过资源目录和不同语言的本地化设置,实现应用程序的多语言支持。通过在res目录下创建语言特定的值目录,并将字符串资源保存在相应的xml文件中,Android系统会根据设备的当前语言环境加载适当的语言资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This class teaches you to

It’s always a good practice to extract UI strings from your app code and keep themin an external file. Android makes this easy with a resources directory in each Androidproject.

If you created your project using the Android SDKTools (read Creating anAndroid Project), the tools create a res/ directory in the top level ofthe project. Within thisres/ directory are subdirectories for various resourcetypes. There are also a few default files such asres/values/strings.xml, which holdsyour string values.

Create Locale Directories and String Files


To add support for more languages, create additional values directories insideres/ that include a hyphen and the ISO country code at the end of thedirectory name. For example,values-es/ is the directory containing simpleresourcess for the Locales with the language code "es". Android loads the appropriate resourcesaccording to the locale settings of the device at run time.

Once you’ve decided on the languages you will support, create the resource subdirectories andstring resource files. For example:

MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml

Add the string values for each locale into the appropriate file.

At runtime, the Android system uses the appropriate set of string resources based on thelocale currently set for the user's device.

For example, the following are some different string resource files for different languages.

English (default locale), /values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
</resources>

Spanish, /values-es/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mi Aplicación</string>
    <string name="hello_world">Hola Mundo!</string>
</resources>

French, /values-fr/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mon Application</string>
    <string name="hello_world">Bonjour le monde !</string>
</resources>

Note: You can use the locale qualifier (or anyconfiguration qualifer) on any resource type, such as if you want to providelocalized versions of your bitmap drawable. For more information, seeLocalization.

Use the String Resources


You can reference your string resources in your source code and other XML files using theresource name defined by the<string> element's name attribute.

In your source code, you can refer to a string resource with the syntax R.string.<string_name>. There are a variety of methods that accept a string resource thisway.

For example:

// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);

// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);

In other XML files, you can refer to a string resource with the syntax @string/<string_name> whenever the XML attribute accepts a string value.

For example:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值