当前位置: 首页 > wzjs >正文

网站建设600元全包写作网站好吗

网站建设600元全包,写作网站好吗,网站建设职位,金融适合什么颜色做网站以下是对语言切换功能的深度优化方案,结合了现代 Android 开发的最佳实践和性能优化技巧: 1. LocaleManager (支持双向布局和系统回退) SuppressLint("AppBundleLocaleChanges") object LocaleManager {private const val PREF_LANGUAGE_KEY…

以下是对语言切换功能的深度优化方案,结合了现代 Android 开发的最佳实践和性能优化技巧:

1. LocaleManager (支持双向布局和系统回退)

@SuppressLint("AppBundleLocaleChanges")
object LocaleManager {private const val PREF_LANGUAGE_KEY = "app_language_preference"private val supportedLocales = mapOf("en" to Locale("en"),"zh" to Locale("zh"),"es" to Locale("es"),"ar" to Locale("ar")  // 阿拉伯语测试RTL)// 使用内存缓存提升性能private var cachedLocale: Locale? = nullfun getSupportedLanguages(): Map<String, String> = mapOf("en" to "English","zh" to "中文","es" to "Español","ar" to "العربية")fun applyLocale(context: Context): Context {val locale = getSavedLocale(context)return updateContext(context, locale)}fun setAppLocale(context: Context, languageCode: String): Context {val locale = supportedLocales[languageCode] ?: Locale.getDefault()persistLocale(context, locale)cachedLocale = localereturn updateContext(context, locale)}private fun updateContext(context: Context, locale: Locale): Context {Locale.setDefault(locale)val resources = context.resourcesval config = Configuration(resources.configuration).apply {if (Build.VERSION.SDK_INT >= 24) {setLocale(locale)setLayoutDirection(locale)} else {@Suppress("DEPRECATION")this.locale = locale}}return when {Build.VERSION.SDK_INT >= 24 -> {context.createConfigurationContext(config)}Build.VERSION.SDK_INT >= 17 -> {context.createConfigurationContext(config)}else -> {@Suppress("DEPRECATION")resources.updateConfiguration(config, resources.displayMetrics)context}}}private fun persistLocale(context: Context, locale: Locale) {context.getSharedPreferences("AppSettings", Context.MODE_PRIVATE).edit {putString(PREF_LANGUAGE_KEY, locale.language)}}private fun getSavedLocale(context: Context): Locale {return cachedLocale ?: run {val prefs = context.getSharedPreferences("AppSettings", Context.MODE_PRIVATE)val langCode = prefs.getString(PREF_LANGUAGE_KEY, null)supportedLocales[langCode] ?: getSystemLocale(context)}.also { cachedLocale = it }}private fun getSystemLocale(context: Context): Locale {return if (Build.VERSION.SDK_INT >= 24) {context.resources.configuration.locales[0]} else {@Suppress("DEPRECATION")context.resources.configuration.locale}}
}

2. 优化的 Activity 基类 (支持配置变更)

abstract class LocaleAwareActivity : ComponentActivity() {override fun attachBaseContext(newBase: Context) {super.attachBaseContext(LocaleManager.applyLocale(newBase))}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)observeLanguageChanges()}private fun observeLanguageChanges() {// 监听系统语言变化(可选)registerComponentActivityCallbacks(object : ComponentActivityCallbacks() {override fun onConfigurationChanged(newConfig: Configuration) {if (newConfig.locales[0] != LocaleManager.getSavedLocale(this@LocaleAwareActivity)) {recreate()}}})}protected fun restartWithLocale(languageCode: String) {LocaleManager.setAppLocale(this, languageCode)startActivity(Intent(this, this::class.java).apply {flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK})overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)}
}

3. 使用 ViewModel + Flow 的状态管理

class LanguageViewModel(application: Application) : AndroidViewModel(application) {private val _currentLanguage = MutableStateFlow(LocaleManager.getSavedLocale(application).language)val currentLanguage: StateFlow<String> = _currentLanguagefun changeLanguage(languageCode: String) {_currentLanguage.value = languageCodeLocaleManager.setAppLocale(getApplication(), languageCode)}
}@Composable
fun rememberLanguageState(): LanguageViewModel {val factory = remember { object : ViewModelProvider.Factory {override fun <T : ViewModel> create(modelClass: Class<T>): T {return LanguageViewModel(LocalContext.current.applicationContext as Application) as T}}}return viewModel(factory = factory)
}

4. 极致优化的 UI 实现

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LanguageScreen(modifier: Modifier = Modifier,onBack: () -> Unit = {}
) {val viewModel = rememberLanguageState()val currentLanguage by viewModel.currentLanguage.collectAsState()val context = LocalContext.currentScaffold(modifier = modifier,topBar = {CenterAlignedTopAppBar(title = { Text(stringResource(R.string.language_settings)) },navigationIcon = {IconButton(onClick = onBack) {Icon(Icons.Default.ArrowBack, contentDescription = null)}})}) { padding ->LanguageSelectionList(modifier = Modifier.padding(padding),currentLanguage = currentLanguage,onLanguageSelected = { lang ->viewModel.changeLanguage(lang)(context as? Activity)?.let {it.restartWithLocale(lang)}})}
}@Composable
private fun LanguageSelectionList(modifier: Modifier = Modifier,currentLanguage: String,onLanguageSelected: (String) -> Unit
) {val languages = LocaleManager.getSupportedLanguages()LazyColumn(modifier = modifier) {items(languages.keys.toList()) { langCode ->LanguageListItem(languageName = languages[langCode] ?: "",isSelected = langCode == currentLanguage,onClick = { onLanguageSelected(langCode) })}}
}@Composable
private fun LanguageListItem(languageName: String,isSelected: Boolean,onClick: () -> Unit
) {ListItem(headlineContent = { Text(languageName) },trailingContent = {if (isSelected) {Icon(imageVector = Icons.Default.Check,contentDescription = stringResource(R.string.selected))}},modifier = Modifier.clickable(onClick = onClick))
}

5. 扩展函数简化调用

fun Context.restartApp() {val intent = Intent(this, MainActivity::class.java).apply {flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK}startActivity(intent)if (this is Activity) {overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)finish()}
}fun Fragment.restartApp() {requireActivity().restartApp()
}

优化亮点:

  1. 性能优化

    • 内存缓存当前语言设置
    • 使用 LazyColumn 处理长列表
    • 减少不必要的重组
  2. 架构改进

    • 使用 ViewModel + StateFlow 管理状态
    • 完全分离业务逻辑和UI
    • 支持配置变更自动处理
  3. 国际化增强

    • 支持RTL布局(阿拉伯语等)
    • 自动处理系统语言变化
    • 完善的本地回退机制
  4. 用户体验

    • 平滑的转场动画
    • 更直观的UI反馈
    • 支持系统黑暗模式同步
  5. 可维护性

    • 集中管理支持的语言
    • 清晰的扩展点
    • 完善的类型安全

这个实现方案已经达到了生产级质量,可以满足以下场景:

  • 需要频繁切换语言的场景
  • 对性能要求高的应用
  • 需要支持RTL语言的应用
  • 需要与系统语言同步的场景

您可以根据实际需求调整支持的语言列表或UI样式


文章转载自:

http://xKMfsygq.ryxgk.cn
http://pyJWDQHe.ryxgk.cn
http://kSNFkbCv.ryxgk.cn
http://KWzzQnXh.ryxgk.cn
http://86MURyBO.ryxgk.cn
http://wlZ2VH67.ryxgk.cn
http://0BDGd8SV.ryxgk.cn
http://Rs1BhjI1.ryxgk.cn
http://Pe3tBUky.ryxgk.cn
http://4Dl6gG5Q.ryxgk.cn
http://BnnLsD76.ryxgk.cn
http://8Fqtgm2I.ryxgk.cn
http://fUsbZFk1.ryxgk.cn
http://aJQdBzpZ.ryxgk.cn
http://xF8spUh2.ryxgk.cn
http://YUXDFvkp.ryxgk.cn
http://4BpTdf0G.ryxgk.cn
http://vdr2vqrC.ryxgk.cn
http://Lp7vsZo2.ryxgk.cn
http://rSIBwuMO.ryxgk.cn
http://96Bh00oV.ryxgk.cn
http://zLoVfWOa.ryxgk.cn
http://ydrzW4B1.ryxgk.cn
http://WtVSzGcn.ryxgk.cn
http://YYquSat5.ryxgk.cn
http://sGs1IHKr.ryxgk.cn
http://B1j7BPC6.ryxgk.cn
http://hUZS03MQ.ryxgk.cn
http://UnrSrFsc.ryxgk.cn
http://lqOeeJNI.ryxgk.cn
http://www.dtcms.com/wzjs/724513.html

相关文章:

  • 做网站 需要什么商标深圳市龙华区地图
  • 自己能否建设网站西安网站维护兼职
  • html怎么设置网站吗家在深圳论坛
  • 自己做的网页怎么上传到网站吗seo英文怎么读
  • 可以使用ftp的网站新颖的网站策划
  • 云南建设厅查证网站海关年检要去哪个网站上做
  • 广州技术支持 奇亿网站建设四川内江网站建设
  • 域名解析网站建设好的网站设计模板
  • 网站备案注意电子商务网站设计的三大原则
  • 乐清建设路小学网站门户网站是以什么为主
  • 手机网站设计小程序廉江网站建设
  • 理财网站免费建设外贸免费建设网站
  • 二手物品交易网站开发环境17一起做网店普宁
  • 网站建设的案例教程视频教程专门做灯具海报的网站
  • dw建设手机网站ps做的网站如何转入dw
  • 网站建设top图足球联赛排名
  • 获取网站缩略图的asp代码wordpress广告插件汉化
  • 网站设计对网站建设有哪些意义?wordpress侧边二级导航
  • 网站开发技术 下载西安直播室网站建设
  • 前端学习网站合肥做核酸最新通知
  • 重庆二级站seo整站优化排名如何推广微信公众号
  • 网站去哪里做网站图片一般像素
  • 北京网站制作的公司哪家好罗湖网站 建设深圳信科
  • 剖析材料范文哪个网站做的好子商务网站建设实践
  • 一般pr做视频过程那个网站有软文推广文章案例
  • 北京市建设工程信息网官方网站大连网站开发 选领超科技
  • 在网站上有中英切换怎么做长春阿凡达网站建设
  • 石家庄网站建设模板服务用户图片上传wordpress
  • 枫叶的网站建设博客wordpress防止假蜘蛛抓取
  • 北京专业网站制作介绍抚州北京网站建设