【android基础知识】【国际化及语言选择】

参考源码:http://download.csdn.net/detail/barryhappy/7176709

将使用support.v7的项目修改了一下,变成了一个不使用actionbaractivity而是activity的。减少报错。

http://download.csdn.net/detail/mcdullsin/8290923

 

一、在说多语言的时候首先说说如何减少“hard code”

什么是“hard code”,例如:java文件中直接使用了汉字字符串等。正确做法是将它放在资源文件中,并在需要的地方进行引用。

说一说如何在java文件等地方使用string.xml中的资源:

 

获取string.xml文件里面的值有几个不同的地方。

1.在AndroidManifest.xml与layout等xml文件里:

android:text="@string/resource_name" 

  

2.在activity里:

方法一:this.getString(R.string.resource_name);  

方法二:getResources().getString(R.string.resource_name); 

 

3.在其他java文件(必须有Context或pplication)

方法一: context.getString(R.string.resource_name); 

方法二: application.getString(R.string.resource_name);  

 

二、接下来分析多语言的源码及如何使用

首先效果截图:

技术分享

技术分享

 

这位大哥的博客中很好地说明了android多语言国际化:

http://www.cnblogs.com/bluestorm/archive/2013/04/01/2993554.html

 

技术分享

 

android的多语言原理是这样:

它会检查android手机的默认配置,然后到具体的values下面去读取字符串。

那位大哥博文中的话:

在Android工程的res目录下,通过定义特殊的文件夹名称就可以实现多语言支持。比如我们的程序兼容简体中文、英文,在values文件夹中建立默认strings.xml,再建立values-zh-rCN文件夹。

 在每个文件夹里放置一个strings.xml,strings.xml里是各种语言字符串。如果涉及到参数配置类xml文件夹名称也要改成xml-zh、xml。这样在android的系统中进行语言切换,所开发的程序也会跟着切换语言。

特殊的文件夹名!

说明文件夹名每个是固定的。

 

那么分析这篇博文中的项目做了什么事情。

它做的事点击menu会弹出语言设置,然后再进行语言的切换。

Java源码如下:

package barry.demo.multilanguage;

import java.util.Locale;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

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) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Configuration config = getResources().getConfiguration();
        switch (item.getItemId()) {
        case R.id.action_english:
            config.locale = Locale.ENGLISH;
            break;
        case R.id.action_simple_chinses:
            config.locale = Locale.SIMPLIFIED_CHINESE;
            break;
        case R.id.action_traditional_chinese:
            config.locale = Locale.TRADITIONAL_CHINESE;
            break;
        default:
            return true;
        }
        getResources().updateConfiguration(config, getResources().getDisplayMetrics());
        ((TextView)findViewById(R.id.textViewHello)).setText(R.string.hello_world);;
        return true;
    }


}

主布局文件如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="barry.demo.multilanguage.MainActivity"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/textViewHello"
        android:layout_width="match_parent"
        android:layout_height="218dp"
        android:gravity="center"
        android:text="@string/hello_world" />

</FrameLayout>

菜单布局文件如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="barry.demo.multilanguage.MainActivity" >

    <item
        android:id="@+id/action_english"
        android:orderInCategory="100"
        android:title="@string/english"/>
    <item
        android:id="@+id/action_simple_chinses"
        android:orderInCategory="100"
        android:title="@string/simple_chinese"/>
    <item
        android:id="@+id/action_traditional_chinese"
        android:orderInCategory="100"
        android:title="@string/traditional_chinese"/>

</menu>

 

 

在这里关键有一个问题就是:

当点击按钮之后,会对文件进行刷新,是每个字符串textview、listview都要刷新。

getResources().updateConfiguration(config, getResources().getDisplayMetrics());
((TextView)findViewById(R.id.textViewHello)).setText(R.string.hello_world);;

 

如果涉及到服务器就不好刷新了。

而且即使本地文件刷新量也有点大了。

 

所以做法是不能在每个页面都有语言切换按钮,而应该将语言切换按钮放在登陆页。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。