就在最近的context.getResources().updateConfiguration()已在Android API 25中弃用,建议使用上下文.而是createConfigurationContext().
有谁知道createConfigurationContext如何用于覆盖android系统区域设置?
在此之前将通过以下方式完成:
Configuration config = getBaseContext().getResources().getConfiguration();
config.setLocale(locale);
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud) 也就是说,我正在构建一个自定义撰写布局并填充该列表,如下所示
val list = remember { dataList.toMutableStateList()}
MyCustomLayout{
list.forEach { item ->
key(item){
listItemCompose( data = item,
onChange = { index1,index2 -> Collections.swap(list, index1,index2)})
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,并且每当调用 onChange lambda 函数时屏幕都会重新组合,但是当涉及任何项目属性的任何微小更改时,它不会重新组合,详细说明让我们更改上面的 lambda 函数以执行以下操作
{index1,index2 -> list[index1].propertyName = true}
Run Code Online (Sandbox Code Playgroud)
让 lambda 更改列表项的属性不会触发屏幕重组。我不知道这是否是 jetpack compose 中的一个错误,或者我只是采用了错误的方法来解决这个问题,我想从 Android 开发团队那里知道正确的方法。这就是让我问是否有办法强制重组整个屏幕的原因。
android android-layout android-studio android-jetpack-compose
我想动态更改我的应用程序语言,而不需要重新启动以Activity使结果生效。我现在要做的是添加一个可变Boolean状态,它是 switch 并由所有Text元素使用。
要更改语言,我在可单击回调中调用以下代码(我使用该框作为虚拟对象,只是为了测试):
val configuration = LocalConfiguration.current
val resources = LocalContext.current.resources
Box(
modifier = Modifier
.fillMaxWidth()
.height(0.2.dw)
.background(Color.Red)
.clickable {
// to change the language
val locale = Locale("bg")
configuration.setLocale(locale)
resources.updateConfiguration(configuration, resources.displayMetrics)
viewModel.updateLanguage()
}
) {
}
Run Code Online (Sandbox Code Playgroud)
updateLanguage()然后它使用该方法切换语言值
@HiltViewModel
class CityWeatherViewModel @Inject constructor(
private val getCityWeather: GetCityWeather
) : ViewModel() {
private val _languageSwitch = mutableStateOf(true)
var languageSwitch: State<Boolean> = _languageSwitch
fun updateLanguage() {
_languageSwitch.value = !_languageSwitch.value
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,为了更新每个Text可组合项,我需要将 …
android kotlin android-jetpack-compose android-jetpack-compose-text
我正在尝试使用 jetpack compose 函数更改应用程序的区域设置,如下所示
@Composable
fun SetLanguage(position: Int) {
val locale = Locale(
when (position) {
0 -> "ar"
1 -> "en"
2 -> "fr"
else -> {
"ar"
}
}
)
Locale.setDefault(locale)
val configuration = LocalConfiguration.current
configuration.setLocale(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
configuration.setLocale(locale)
else
configuration.locale = locale
var resources = LocalContext.current.resources
resources.updateConfiguration(configuration, resources.displayMetrics)
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看工作示例(没有按钮或文本字段) https://github.com/MakeItEasyDev/Jetpack-Compose-Multi-Language-Support
但问题是不适用于 OutlinedTextField 或 Button,因为调用此函数时它们不会改变,甚至 rightToLeft 支持也不起作用,而且我找不到解决此问题的良好替代方案,因为我无法在项目中重新创建活动