如何覆盖 setContentView 的资源

How to override resourses for setContenView

我做了这个 class 扩展资源:

public class CustomResources extends Resources {

    private static CustomResources instance;

    /**
     * Create a new Resources object on top of an existing set of assets in an
     * AssetManager.
     *
     * @param assets  Previously created AssetManager.
     * @param metrics Current display metrics to consider when
     *                selecting/computing resource values.
     * @param config  Desired device configuration to consider when
     */
    public CustomResources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
        super(assets, metrics, config);
    }

    public static CustomResources getInstance(AssetManager assets, DisplayMetrics metrics, Configuration config) {
        if (instance == null) {
            instance = new CustomResources(assets, metrics, config);
        }

        return instance;
    }

    @Override
    public String getString(int id) throws NotFoundException {
        return super.getString(id) + " $$";
    }
}

我通过以下方式设置了新资源:

@Override
public Resources getResources() {
    return CustomResources.getInstance(super.getAssets(), super.getResources().getDisplayMetrics(), super.getResources().getConfiguration());
}

所以,我不想自定义 resurses 适用于我加载的布局中包含的所有字符串:

setContentView(R.layout.at_site);

但它只适用于加载的字符串:

title.setText(getString(R.string.at_site_at_receiver));

我错过了什么?

您可以使用 Android 的内置替代资源管理(请参阅 Guide to Localization

如果这不符合您的需求,那么我建议您将所有资源声明为数组,并针对每个资源,根据用户的设置选择合适的资源。但这将是一个复杂得多的解决方案。