[android] 01_Android国际化

Android 4.0

Android国际化 

1、组件显示的内容国际化   
values文件夹string.xml
android:text="@string/hello_world" 
2、代码中的内容国际化,如Toast, 
values文件夹string.xml
Toast.makeText(this, getResources().getString(R.string.hello_world),Toast.LENGTH_LONG).show();  
写对了,会出现国旗
 values-en\strings.xml
values-zh\strings.xml
3、图片国际化
drawable目录放默认的图片国际化的图片
android:src="@drawable/flag"   


技巧:
a)values中国际化文件夹的命名  values-国家-地区
如中国台湾
values-zh-rTW


b)到Android源码中查看:packages\apps\Settings\res\文件夹命名


1、布局:
a) \res\layout\activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/hello_world" />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/flag" 
       /> 
</LinearLayout>
b) \res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Android国际化(默认)</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!(默认)</string>
</resources>
2、代码:
package cn.zengfansheng.i18n;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Toast.makeText(thisgetResources().getString(R.string.hello_world),
                Toast.LENGTH_LONG).show();
    }
    @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;
    }
}
3、结果:
中国
中国台湾
美国
其他国家默认

中国

美国

加拿大