在Android开发中如何通过APP实现内语言切换功能
发布时间: 2020-12-04 16:15:45
来源:亿速云
阅读: 104
作者: Leah
在这次的内容中,小编将针对在安卓开发中如何在APP上实现内语言切换功能,以文章内容丰富、专业的角度为大家进行分析和阐述。 读了这篇文章,希望大家能有所收获。
代码实现:
布局文件(数据绑定模式)是一个简单的两行字符
安卓: layout _ width=' match _ parent '
Android : layout _ height=' match _ parent '
tools : context=' com.tnnowu.Android.switch language.main activity '
Android : id=' @ id/title textview '
安卓: layout _ width=' wrap _ content '
安卓: layout _ height=' wrap _ content '
安卓: layout _ centerinparent=' true '
Android : text=' @ string/title '
安卓:文本大小=' 30sp '
安卓:文本样式=' bold ' /
Android : id=' @ id/desc textview '
安卓: layout _ width=' wrap _ content '
安卓: layout _ height=' wrap _ content '
Android : layout _ below=' @ id/title textview '
安卓: layout _ center horizontal=' true '
Android : layout _ margin top=' 10dp '
android:text='@string/desc '
安卓:文本大小=' 20sp ' /
从例子中可以看到,右上角有Menu
xmlns 3360 tools=' http://schemas.Android.com/tools '
tools : context='.main activity '
Android : id=' @ id/language _ English '
安卓:订单管理系统=' 100 '
Android : title=' @ string/menu _ English ' /
Android : id=' @ id/language _ simplified _ Chinese '
安卓:订单管理系统=' 100 '
Android : title=' @ string/menu _ simplified _ Chinese ' /
Android : id=' @ id/language _ Turkish '
安卓:订单管理系统=' 100 '
Android : title=' @ string/menu _ Turkish ' /
Android : id=' @ id/language _ Japanese '
安卓:订单管理系统=' 100 '
Android : title=' @ string/menu _ Japanese ' /
(既然是多语言,就需要n个字符串)
在这个案例中制作了四种语言。
是的,Menu的布局写完了。 接下来是实现Menu的功能。 请记住,实现Menu有两个代码。 一个是onCreateOptionsMenu,另一个是onOptionsItemSelected。
@Override
publicbooleanoncreateoptionsmenu (menu menu ) {
getMenuInflater ().inflate(r.menu.menu,menu );
返回真;
}
@Override
publicbooleanonoptionsitemselected { menuitem item }
int id=item.getItemId (;
if(id==r.id.Language_English ) {
更新视图(en );
} else if (id==r.id.language _ simplified _ chi
nese) {updateViews("zh");
} else if (id == R.id.language_turkish) {
updateViews("tr");
} else if (id == R.id.language_japanese) {
updateViews("ja");
}
return super.onOptionsItemSelected(item);
}
在这里,可以看到,我们自定义一个 updateViews() 方法,用来实现切换预言时界面的改变
private void updateViews(String languageCode) {
Context context = LocaleHelper.setLocale(this, languageCode);
Resources resources = context.getResources();
mBinding.titleTextView.setText(resources.getString(R.string.title));
mBinding.descTextView.setText(resources.getString(R.string.desc));
setTitle(resources.getString(R.string.toolbar_title));
}
公布一个 语言判断的类 LocaleHelper
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
最后还要做的操作就是,自定义一个Application类,用来设定App的默认语言(当然了,要将这个Application应用到Manifest中)
public class BaseApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}
}
上述就是小编为大家分享的Android开发中怎么在App中实现一个内语言切换功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。