用李白的一首诗来做本次演示
language.gif
1.res下新建values-zh (中文文件夹)并且新建一个strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="poetry">朝辞白帝彩云间,\n千里江陵一日还。\n两岸猿声啼不尽,\n轻舟已过万重山。</string>
<string name="language">语言</string>
<string name="title_activity_5">5.多语言适配</string>
</resources>
2.res下新建values-en (英文文件夹)并且新建一个strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="poetry">Energy-saving,\nTrinidad Jiangling day also.\nTwo monkeys do,\nHas been in a canoe.</string>
<string name="language">Language</string>
<string name="title_activity_5">5.Multilingual adaptation</string>
</resources>
3.res/layout下新建activity_5.xml 布局文件 引用资源文件中的string
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setLanguage"
android:layout_marginTop="10dp"
android:textAllCaps="false"
android:text="@string/language"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="@string/poetry"/>
</LinearLayout>
4.新建Activity_5.class
public class _5_Activity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setLanguage();
setContentView(R.layout.activity_5);
setTitle(R.string.title_activity_5);
}
public void setLanguage(View v){
String[] strings = {"简体中文","English"};
//首次进入判断是否是中文语言
int selected = getSharedPreferences("language", Context.MODE_PRIVATE).getInt("language",-1);
if (selected==-1){
String defaultLanguage = Locale.getDefault().toString();
if (defaultLanguage.equals(Locale.CHINESE.toString())
|| defaultLanguage.equals(Locale.SIMPLIFIED_CHINESE.toString())
||defaultLanguage.equals(Locale.TRADITIONAL_CHINESE.toString()) ){
selected = 0;
}else{
selected = 1;
}
}
new AlertDialog.Builder(this)
.setSingleChoiceItems(strings,selected,
new DialogInterface.OnClickListener() {
//点击单选框某一项以后
public void onClick(DialogInterface dialogInterface, int i) {
SharedPreferences preferences = getSharedPreferences("language", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("language",i);
editor.apply();
dialogInterface.dismiss();
//重启activity
Intent intent = new Intent(_5_Activity.this, _5_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
})
.create()
.show();
}
/** 设置语言 */
private void setLanguage() {
SharedPreferences preferences = getSharedPreferences("language", Context.MODE_PRIVATE);
int language = preferences.getInt("language", 0);
//根据读取到存放在sp里面的数据 进行设置
Configuration configuration = getResources().getConfiguration();
switch (language){
case 0:
configuration.setLocale(Locale.CHINESE);
break;
case 1:
configuration.setLocale( Locale.ENGLISH);
break;
}
getResources().updateConfiguration(configuration,getResources().getDisplayMetrics());
}
}
多语言的配置很简单,在于繁琐,当有几百个string的时候...如果您有改进的建议,欢迎在github上提交.
网友评论