Android动态添加项目到数组XML资源

发布于 2025-01-02 02:28:14 字数 73 浏览 2 评论 0原文

有什么方法可以将代码中的项目添加到字符串数组资源中吗?例如,如果我想创建一个显示用户值的微调器,并且我想允许用户添加自己的自定义值。

Is there any way I can add items in code to a String-Array resource? For instance, if I want to create a spinner that shows the user values, and I want to allow the user to add their own custom values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2025-01-09 02:28:14

不。不支持此操作,因为资源打包在二进制 .apk 中,因此无法更改。

不要遵循这种设计模式,改变你的方法。

No. this is not supported because resources are packaged in the binary .apk and as such cannot be changed.

Don't follow this design pattern, change your approach.

挽心 2025-01-09 02:28:14

可能JoxTraex对android框架一无所知,或者正如他们所说:

当有人说这不能完成时 - 总有人不知道这一点并且会这样做:)

所以说:

Resources 是一个开放类,只是应用程序中 ResourcesImpl 类的包装器

(带有已弃用的构造函数 - 但可用)

public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)

,每次对其的调用都是通过 Context 进行的

,因此:

1 )你总是可以提供你自己的资源实现

2)或(当它足够时)你自己的上下文实现(所以天空是极限)

...我现在感觉眼睛在“上下文实现” - 是的,你甚至可以替换LoadedApk,Activity等中最顶层的上下文.. 是吗?但什么时候、如何?如何:通过反射,何时:优秀的应用程序设计者知道这个地方是(之前)第一次/任何对此类对象的调用的位置...

但是这里有一个危险的陷阱 - 在一种情况下,android直接将 Context 解包到 ContextImp,然后你需要返回那些 Context 而不是你的 - 我将为那些喜欢谜语的人保密的地方在哪里:)

资源中还有

a) 一个隐藏的构造函数(最好的入口点,因为它没有生成任何 ReourceImpl (其中可以是通过下面提到的方法设置)

public Resources(@Nullable ClassLoader classLoader) 

b)隐藏方法

setResImpl(ResourcesImpl) 

总结:

因此通过调用

Resources.getStringArray(**not existing in APK resource id here**);

我们自己的实现,我们可以获得想要的东西:)

更好的是,我们可以添加到我们的实现

addResourceById(int,Object)

方法并添加新资源:)然后通过对资源进行分配能力检查,我们将使用并对我们的实现进行向上转换:)以使用我们新添加的方法:)

顺便说一句:如果有人对你说“你不应该这样做 - blablabla”,这就是这样做的最好理由! - 如果法律不允许:)

足够的理论去实践:

示例 Context 实现将调用转发给 getString:

    public class MyContext extends Context {
        ....
    // override context get resources method 
    @Override
    public android.content.res.Resources getResources() {
        // get super resources 
        android.content.res.Resources resources = super.getResources();
        // pull assets 
        android.content.res.AssetManager assets = resources.getAssets();
        // pull metrics 
        android.util.DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        // pull configuration 
        android.content.res.Configuration configuration = resources.getConfiguration();
        // construct new anon resource implementation 
        return new android.content.res.Resources(assets, displayMetrics, configuration) {

            // overrride interesting method 
            @android.support.annotation.NonNull
            @Override
            public String getString(int id) throws android.content.res.Resources.NotFoundException {
                return id == pl.ceph3us.base.common.R.string.my_sweet_google
                    ? "fck_you_google";
                    : super.getString(id);
            }
        };
    }

}

probably JoxTraex has no idea on android framework or as they are saying:

when someone says this can't be done - there always is someone who doesn't know that and will do it :)

so to the point:

Resources is an open class is just a wrapper around ResourcesImpl class

(with depreciated constructor - but is available)

public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)

in app every call to it is made via Context

so:

1) you always can provide your own Resources implementation

2) or (when its enough) your own Context implementation (so sky is the limit)

... i now feel eyes on "Context implementation" - yeah you can even replace top most context in LoadedApk, Activity, etc.. yeah cool? but when, how? how: via reflections, when: good app designer knows that this place is where (before) first/any call to such object is made ...

But there is one dangerous catch here - in one case android straight unwrap Context to ContextImp and then you need to return those Context instead yours - where is the place that i'll keep as secret for those who like riddles :)

there is also in Resources

a) a hidden constructor (best entry point as it is not making any ReourceImpl (which can be set by method mentioned bellow)

public Resources(@Nullable ClassLoader classLoader) 

b) hidden method

setResImpl(ResourcesImpl) 

sum up:

so by calling

Resources.getStringArray(**not existing in APK resource id here**);

on our own implementation we could get whatcha want :)

even better we can add to our implementation

addResourceById(int,Object)

method and add new resources :) then with assign ability check on resource we will use and do a up casting to our implementation :) to use our newly added method :)

btw: if someone says to you "you shouldn't do it - blablabla" this is the best reason to do it! - if not permitted by law :)

enough the theory go to a practice:

example Context implementation forwards calls to getString:

    public class MyContext extends Context {
        ....
    // override context get resources method 
    @Override
    public android.content.res.Resources getResources() {
        // get super resources 
        android.content.res.Resources resources = super.getResources();
        // pull assets 
        android.content.res.AssetManager assets = resources.getAssets();
        // pull metrics 
        android.util.DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        // pull configuration 
        android.content.res.Configuration configuration = resources.getConfiguration();
        // construct new anon resource implementation 
        return new android.content.res.Resources(assets, displayMetrics, configuration) {

            // overrride interesting method 
            @android.support.annotation.NonNull
            @Override
            public String getString(int id) throws android.content.res.Resources.NotFoundException {
                return id == pl.ceph3us.base.common.R.string.my_sweet_google
                    ? "fck_you_google";
                    : super.getString(id);
            }
        };
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文