Android应用内多语言切换(国际化)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010218170/article/details/83422787

1、创建多语言文件

 2、设置每个strings.xml文件中的值

3、使用strings.xml中的值

//代码中使用,Activity、Fragment直接使用
String value = getString(R.string.chinese);
//xml布局中使用
<TextView
    android:id="@+id/chinese"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chinese"/>

 4、应用内语言切换核心代码

// 多语言设置
Locale myLocale = new Locale(Locale.CHINESE.getLanguage());
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
recreate();//重启activity,显示选择语言的文字

运行截图 (代码下载)

猜你喜欢

转载自blog.csdn.net/u010218170/article/details/83422787