Android开发中怎么在应用中实现一个内语言切换功能

  介绍

这期内容当中小编将会给大家带来有关Android开发中怎么在应用中实现一个内语言切换功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

<强>代码实现:

布局文件(数据绑定模式),很简单就是两行文字

& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   & lt;布局xmlns: android=癶ttp://schemas.android.com/apk/res/android"比;   http://schemas.android.com/tools" & lt; RelativeLayout xmlns:工具=?;   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   工具:上下文=癱om.tnnowu.android.switchlanguage.MainActivity"比;   & lt; TextView   android: id=癅 + id/titleTextView"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_centerInParent=皌rue"   android:文本=癅string/title"   android: textSize=?0 sp"   android: textStyle=癰old"/比;   & lt; TextView   android: id=癅 + id/descTextView"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_below=癅 + id/titleTextView"   android: layout_centerHorizontal=皌rue"   android: layout_marginTop=?0 dp"   android:文本=癅string/desc"   android: textSize=?0 sp"/比;   & lt;/RelativeLayout>   & lt;/layout>

从实例中我们可以看到右上角是有菜单

& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt;菜单xmlns: android=?;   xmlns:工具=癶ttp://schemas.android.com/tools"   工具:上下文=?MainActivity"比;      & lt;项目   android: id=癅 + id/language_english"   android: orderInCategory=?00”;   android: title=癅string/menu_english"/比;   & lt;项目   android: id=癅 + id/language_simplified_chinese"   android: orderInCategory=?00”;   android: title=癅string/menu_simplified_chinese"/比;   & lt;项目   android: id=癅 + id/language_turkish"   android: orderInCategory=?00”;   android: title=癅string/menu_turkish"/比;   & lt;项目   android: id=癅 + id/language_japanese"   android: orderInCategory=?00”;   android: title=癅string/menu_japanese"/比;   & lt;/menu>

(既然是多语言,所以就要有N个字符串)

 Android开发中怎么在应用中实现一个内语言切换功能

本案例我创建了4种语言。

好的,菜单的布局写完了,接下来就是实现菜单功能,记住实现菜单就两套代码,一个> @Override   公共布尔>私人空updateViews(字符串languageCode) {   上下文语境=LocaleHelper。languageCode setLocale ();   资源资源=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

公共类LocaleHelper {
  私有静态最终字符串SELECTED_LANGUAGE=癓ocale.Helper.Selected.Language";
  公共静态上下文>公共类BaseApplication扩展应用程序{
  @Override
  保护无效attachBaseContext(上下文基地){
  super.attachBaseContext (LocaleHelper。onAttach(基地,“en"));
  }
  }

上述就是小编为大家分享的Android开发中怎么在应用中实现一个内语言切换功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

Android开发中怎么在应用中实现一个内语言切换功能