kotlin compose 实现应用内多语言切换(不重新打开App)
创始人
2024-12-28 06:38:50
0

1. 示例图

2.具体实现

如何实现上述示例,且不需要重新打开App

①自定义MainApplication 实现Application ,定义两个变量:

class MainApplication : Application() {     object GlobalDpData {     var language: String = ""      var defaultLanguage: String = "en" } override fun onCreate() {         defaultLanguage = Locale.getDefault().language         val cacheLanguage = "保存在手机App本地的切换后的语言,可用sp或文件实现"          language = if (cacheLanguage.isNullOrEmpty()) {         defaultLanguage         } else  {         cacheLanguage         }      } }

②在baseActivity中添加语言实现

override fun attachBaseContext(baseContext: Context) {     var language = MainApplication.GlobalDpData.language     if (language.isEmpty()) {         val default = MainApplication.GlobalDpData.defaultLanguage         language = "保存在手机App本地的切换后的语言,可用sp或文件实现"      }     // 创建一个ContextWrapper对象     val context = newWrap(baseContext, language)     // 将新的的Context设置给Activity     super.attachBaseContext(context) }
/**  *  创建ContextWrapper对象,  */ private fun newWrap(context: Context, language: String): ContextWrapper {     val configuration = context.resources.configuration     configuration.fontScale = 1f     val locale = Locale(language)     val localeList = LocaleList(locale)     LocaleList.setDefault(localeList)     configuration.setLocales(localeList)     

相关内容

热门资讯

辅助透视!aapoker破解侠... 辅助透视!aapoker破解侠是真的吗,aapoker透视插件,2025新版(有挂解说)1、辅助透视...
透视黑科技!wepoker底牌... 透视黑科技!wepoker底牌透视(透视)一直是有挂(系统教程);1、wepoker底牌透视ai辅助...
透视肯定!aapoker插件,... 透视肯定!aapoker插件,aapoker透视脚本入口,微扑克教程(有挂脚本);1、进入到aapo...
透视辅助!aapoker公共底... 透视辅助!aapoker公共底牌,aapoker透视脚本入口,2025新版教程(有挂教程)1、打开软...
透视游戏!wepoker永久免... 透视游戏!wepoker永久免费脚本(透视)原来存在有挂(必赢教程)1、构建自己的wepoker永久...
广东雀神智能辅助照片,兴化打两... 广东雀神智能辅助照片,兴化打两圈辅助器,微信小程序边锋辅助攻略1、完成广东雀神智能辅助照片的残局,帮...
透视ai代打!aapoker辅... 透视ai代打!aapoker辅助器是真的吗,aapoker插件下载,插件教程(有挂方法)1、让任何用...
透视能赢!wepoker作弊方... 透视能赢!wepoker作弊方法(透视)确实是有挂(总结教程)1、超多福利:超高返利,海量正版游戏,...
广东雀神挂件去那买,广西友乐辅... 广东雀神挂件去那买,广西友乐辅助app,财神13张辅助系统解密1、在广东雀神挂件去那买ai机器人技巧...
透视透视!aapoker发牌逻... 透视透视!aapoker发牌逻辑,aapoker万能辅助器,微扑克教程(有挂解密)1、aapoker...