Load resource (layout) from another apk dynamically

前端 未结 3 2179
日久生厌
日久生厌 2021-02-10 02:37

I managed to pull the layouts, and i add it to my viewflipper, however, there it is loaded as blank.

The code is,

Resources packageResources;
Context pac         


        
3条回答
  •  南旧
    南旧 (楼主)
    2021-02-10 03:15

    I am currently doing this. It only works if a know the packageName of activity or fragment from the .apk that should provide the layout hierarchy (I call this foreign context). And you need to know the name of the layout you want to inflate (e.g. R.layout.mylayout -> "mylayout")

    Context c = createPackageContext(foreignPackageName,
        Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY); //create foreign context
    int resId = c.getResources.getIdentifier(mylayoutName,"layout",foreignPackageName); 
    LayoutInflater myInflater = LayoutInflater.from(c); //Inflater for foreign context
    View myLayout = myInflater.inflate(resId,null,false); //do not attach to a root view
    

提交回复
热议问题