求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   模型库  
会员   
 


AI 智能化软件测试方法与实践
5月23-24日 上海+在线



人工智能.机器学习TensorFlow
5月22-23日 北京



图数据库与知识图谱
5月22-23日 北京
 
 
 

Android开发教程
Android 开发环境配置
Android 架构
Android 应用组件
Android Hello World示例
Android 资源组织和访问
Android Activity
Android Service
Android 广播接收器
Android 内容提供者
Android 碎片/片段
Android Intent过滤器
Android UI布局
Android UI控件
Android 事件处理
Android 样式和主题
Android 自定义组件
Android 拖放
Android 通知
Android 基于位置服务
Android 发送电子邮件
Android 发送短信/SMS
Android 拨打电话
发布Android应用
ndroid Alertdialog
Android Animation实例
Android音频捕获
Android音频管理器实例
Android
Android最佳实践
Android Bluetooth实例
Android Camera
Android Clipboard
Android自定义字体
Android数据备份
Android Gestures/手势
Android图片效果
Android图片切换
Android内部存储
Android JetPlayer实例
Android JSON解析器
Android加载Spinner
Android本地化
Android登录实例
Android MediaPlayer
 
 

Android本地化
1093 次浏览
52次  

Android应用程序可以在许多不同地区的许多设备上运行。为了使应用程序更具交互性,应用程序应该处理以适合应用程序将要使用的语言环境方面的文字,数字,文件等。

在本章中,我们将解释,如何根据不同区域等应用程序我们将本地化的应用中定位使用的字符串,并以同样的方式的其他东西可以本地化。

本地化字符串

为了在你的应用程序中使用的字符串进行本地化,使RES下一个新的文件夹名称为本地值(values-local),当地将被替换的区域。

例如,在意大利的情况下,值在(values-it)文件夹将在res下。这显示在下面的图片:

该文件夹制成之后,从默认的文件夹中的strings.xml复制到所创建的文件夹。并更改其内容。举例来说,已经改变参考hello world字符串的值。

意大利, RES/VALUES-IT/STRINGS.XML

<;?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello_world">Ciao mondo!</string>  </resources>

西班牙, RES/VALUES-IT/STRINGS.XML

<;?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello_world">Hola Mundo!</string>
    </resources>

法国,RES/VALUES-IT/STRINGS.XML

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

除了这些语言,其他语言的区域码已在表中给出:

例子

这里有一个例子演示如何使用字符串的本地化的。它创建了一个基本的应用程序,允许根据美国和意大利地区的自定义应用程序。

为了试验这个例子,可以在实际设备或模拟器运行此应用程序。

以下是修改后的主活动文件的内容?src/com.yiibai.locals/MainActivity.java.

package com.example.locals;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    public class MainActivity extends Activity {
       @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     }
       @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
     }
    }

以下是修改?res/layout/activity_main.xml?的内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".MainActivity" >
       <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="174dp"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>

以下是?res/values/string.xml. 的内容

<?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="app_name">Locals</string>
       <string name="action_settings">Settings</string>
       <string name="hello_world">Hello world!</string>
    </resources>

以下是?res/values-it/string.xml. 的内容

<?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="app_name">Locals</string>
       <string name="action_settings">Settings</string>
       <string name="hello_world">Ciao mondo!</string>
    </resources>

以下是 AndroidManifest.xml 文件的内容

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.yiibai.locals"
     android:versionCode="1"
     android:versionName="1.0" >
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
       <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
        <activity
           android:name="com.yiibai.locals.MainActivity"
           android:label="@string/app_name" >
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>
     </application>
  </manifest>

让我们试着来运行修改本地化应用。安装程序AVD并启动它,如果一切设置和应用程序都没有问题,它会显示以下仿真器窗口:

现在从菜单/系统设置/语言意大利改变设置设备的语言。

现在再次打开应用程序,这时候会看到的Hello World在意大利语言。它已被证明下面::



您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1093 次浏览
52次
 捐助