在一些应用需要国际化或者面对各种用户群体时,会涉及到语言的不同,所以我们在开发应用apk时,记得一定要养成将应用中涉及到的所有文字信息放在string.xml里的好习惯!Android中的多语言切换,对于我们应用层的开发同学来时,操作起来非常见到,代码也很简单,大部分工作都是让framwork层的资源进程调度框架给代工了。OK,下面我们一步步看看怎么完成多语言的配置和切换:
1.右击res目录,打开对应的对话框:
2.选择local选项,然后点击右推的按钮,创建和选择新的语言资源文件,注意以下这几个地方需要正确填写:
然后点击ok就会开发项目目录中多了一个文件夹和文件:
3.接着把默认的values/strings.xml中的内容全部复制到values-en/strings.xml中,然后把内容中的文字顺便翻译成对应的语言:
4.接下来就是在activity中引用和设置的操作了:
首先需要定义一个全局变量存放activity栈和选择的语言类别:
public class Constants {
//系统默认是zh的类型
public static String langae = "zh";
public static List<Activity> activityList = new ArrayList<>();
}
MainActivity.java中:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Constants.activityList.add(this);
switchLanguage(Constants.langae);
}
public void onclick(View view) {
startActivity(new Intent(this, SecendActivity.class));
}
//核心设置的代码
protected void switchLanguage(String language) {
Resources resources = getResources();
Configuration config = resources.getConfiguration();
DisplayMetrics dm = resources.getDisplayMetrics();
switch (language) {
case "zh":
config.locale = Locale.CHINESE;
resources.updateConfiguration(config, dm);
break;
case "en":
config.locale = Locale.ENGLISH;
resources.updateConfiguration(config, dm);
break;
default:
config.locale = Locale.US;
resources.updateConfiguration(config, dm);
break;
}
}
}
在onCreate()中设置语言的默认值。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorPrimary"
android:onClick="onclick"
android:padding="10dp"
android:text="@string/lanage"
android:textColor="#fff" />
</RelativeLayout>
SecendActivity.xml:
public class SecendActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atty_secend_layout);
Constants.activityList.add(this);
RadioGroup radio = (RadioGroup) findViewById(R.id.radio);
final RadioButton radioButtonen = (RadioButton) findViewById(R.id.radio_en);
final RadioButton radioButtonzh = (RadioButton) findViewById(R.id.radio_zh);
if (Constants.langae.equals("zh")) {
radioButtonzh.setChecked(true);
} else {
radioButtonen.setChecked(true);
}
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radio_zh) {
Constants.langae = "zh";
radioButtonen.setChecked(false);
} else {
radioButtonzh.setChecked(false);
Constants.langae = "en";
}
}
});
}
public void onClick(View view) {
Locale locale = new Locale(Constants.langae);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
//让之前打开的所有界面全部彻底关闭
for (Activity activity : Constants.activityList) {
activity.finish();
}
//回到应用的首页
startActivity(new Intent(this, MainActivity.class));
}
}
atty_secend_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_zh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/zh" />
<RadioButton
android:id="@+id/radio_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/en" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorPrimary"
android:onClick="onClick"
android:padding="10dp"
android:text="@string/click"
android:textColor="#fff" />
</RelativeLayout>
5.写完代码后,我们运行看看:
设置前:
设置后: