Giter Club home page Giter Club logo

Comments (4)

afkT avatar afkT commented on May 22, 2024

我之前写了个工具类,就是通过包名,反射获取资源id,value,这个你要么,要的话我回头整理下发你,你塞里吧

哈哈,你发下,我看下具体实现什么功能,有必要的话可以放进去,目前有获取资源 id 的 ResourceUtils getIdentifier 方法

    /**
     * 获取资源 id
     * @param resName     资源名
     * @param defType     资源类型
     * @param packageName 应用包名
     * @return 资源 id
     */
    public static int getIdentifier(final String resName, final String defType, final String packageName) {
        try {
            return DevUtils.getContext().getResources().getIdentifier(resName, defType, packageName);
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "getIdentifier - " + resName + " " + defType + ": " + packageName);
        }
        return 0;
    }

from devutils.

JiZhi-Error avatar JiZhi-Error commented on May 22, 2024

稍微晚几天,在外地

from devutils.

JiZhi-Error avatar JiZhi-Error commented on May 22, 2024
public class ResourcesUtils {

    private AssetManager mAsset = AssetManager.class.newInstance();
    private Method addAssetPath = mAsset.getClass().getMethod("addAssetPath", String.class);
    private Context context;
    private Resources pluginResources;
    private String packageName;

    public ResourcesUtils() throws InstantiationException, IllegalAccessException, NoSuchMethodException {
    }

    public void init(Context context, String fileUrl) throws InvocationTargetException, IllegalAccessException {
        this.context = context;
        pluginResources = new Resources(mAsset, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
        //TODO 此处需要先判断 fileUrl 是否有文件
        addAssetPath.invoke(mAsset, fileUrl);
        PackageInfo packageArchiveInfo = context.getPackageManager().getPackageArchiveInfo(
                fileUrl,
                PackageManager.GET_ACTIVITIES
        );
        packageName = packageArchiveInfo.packageName;
    }

    public String getString(String stringName){
        int stringId = pluginResources.getIdentifier(stringName, "string", packageName);
        return pluginResources.getString(stringId);
    }
}

from devutils.

afkT avatar afkT commented on May 22, 2024

已经实现了 ResourcePluginUtils.java,过几天再发个版本,上次的日历工具类:CalendarUtils.java

from devutils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.