Android-如何在运行时注入values/strings.xml

Android-如何在运行时注入values/strings.xml,android,Android,假设我有一个RESTAPI,它允许我获取strings.xml文件。 下载后是否可以将其设置为应用程序的默认值/strings.xml文件?字符串资源是静态定义的,不能在运行时更改 但是,您可以使用库在运行时通过添加依赖项来设置和加载字符串: // Replace bundled strings dynamically implementation 'dev.b3nedikt.restring:restring:4.0.3' // Intercept view inflation implem

假设我有一个RESTAPI,它允许我获取
strings.xml
文件。
下载后是否可以将其设置为应用程序的默认
值/strings.xml
文件?

字符串资源是静态定义的,不能在运行时更改

但是,您可以使用库在运行时通过添加依赖项来设置和加载字符串:

// Replace bundled strings dynamically
implementation 'dev.b3nedikt.restring:restring:4.0.3'

// Intercept view inflation
implementation 'io.github.inflationx:viewpump:2.0.3'

// Allows to update the text of views at runtime without recreating the activity
implementation 'dev.b3nedikt.reword:reword:1.1.0'
在应用程序类中初始化它:

Restring.init(this,
        new RestringConfig.Builder()
                .stringsLoader(new SampleStringsLoader())
                .loadAsync(false) // If string loader should load strings asynchronously, default true
                .build()
        );

ViewPump.init(ViewPump.builder()
        .addInterceptor(RewordInterceptor.INSTANCE)
        .build());
然后应将其注入上下文:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(ViewPumpContextWrapper.wrap(Restring.wrapContext(newBase)));
}

@Override
public Resources getResources() {
    return Restring.wrapContext(getBaseContext()).getResources();
}
您可以随时加载更多字符串:

// e.g. locale=Locale.EN newStrings=map of (key-value)s
Restring.setStrings(locale, newStrings);
并在不重新启动应用程序的情况下应用更新的资源:

// The layout containing the views you want to localise
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
Reword.reword(rootView);

更多详细信息:

您为什么要这样做。在应用程序启动和资源加载之间,将向用户显示什么样的资源?请检查,从答案看,您似乎无法做到这一点