gpt4 book ai didi

c# - Android Resources.UpdateConfiguration(Configuration, DisplayMetrics) 弃用

转载 作者:行者123 更新时间:2023-11-30 22:55:04 24 4
gpt4 key购买 nike

编辑 2

sample

编辑

覆盖 attachBaseContext 方法以更新 Activity 中的上下文

protected override void AttachBaseContext(Context @base)
{
base.AttachBaseContext(@base);
}

结束编辑

在 Android API 25 中,Resources.UpdateConfiguration(Configuration, DisplayMetrics) 已弃用,建议改用 Context context = CreateConfigurationContext(Configuration);

当前实现

public override Resources Resources
{
get
{
Resources res = base.Resources;
Configuration config = new Configuration();
config.SetToDefaults();

res.UpdateConfiguration(config, res.DisplayMetrics);
return res;
}
}

引用 Android context.getResources.updateConfiguration() deprecated作为指导,尝试了以下方法:

public override Resources Resources
{
get
{
Configuration overrideConfiguration = base.Resources.Configuration;
overrideConfiguration.SetToDefaults();
Context context = CreateConfigurationContext(overrideConfiguration);
Resources res = context.Resources;
return res;
}
}

但是这会产生异常错误..

Android.Views.InflateException: Error inflating class
com.android.internal.widget.DialogTitle

如何正确实现Context context = CreateConfigurationContext(Configuration)

请注意“当前实现”工作得很好,但鼓励不要使用已弃用的代码,希望替换正常工作

最佳答案

不确定是否是你的需要,你可以像这样改变attachBaseContext方法,它可以工作:

protected override void AttachBaseContext(Context @base)
{
Configuration overrideConfiguration = new Configuration();
overrideConfiguration = @base.Resources.Configuration;
overrideConfiguration.SetToDefaults();
Context context = @base.CreateConfigurationContext(overrideConfiguration);
base.AttachBaseContext(context);
}

关于c# - Android Resources.UpdateConfiguration(Configuration, DisplayMetrics) 弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55621991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com