SoFunction
Updated on 2025-04-09

Android loads Xml layout file in Assets directory

Recently, because project development has used dynamic layout, the activity of sdk requires some layout files because of packaging sdk. Friends who have done SDK development should know that the layout file cannot be packaged into the jar. Except for aar, of course. Since the project still uses jar packages, how to solve the layout file is a problem. The solution I thought of at the beginning was to send the layout file to the customer. However, this method is obviously not suitable. Later, I discovered that Android actually provides a method to load the XML layout file, which is to useinflate(XmlPullParser parser, ViewGroup root)I found a large number of articles online, and found two articles that simply describe the analysis process, but there were still several problems during use:

1 How to get the XmlPullParser object

It is not difficult to get this object, we can easily obtain it through AssetsMangerXmlResourceParser openXmlResourceParser(String fileName)
But note that there is a problem here, filename must be prefixed with "assets\" or it will report FileNotFound exception.

2 I found that the xml layout file cannot be parsed

The openxmlresourceparser method reports an error, why? The information was found because this method can only parse the compiled xml file. So what is the compiled xml file? The generated apk is decompressed. The xml obtained after decompression is compiled. So the xml we put in assets must be compiled files. There are no other tools found for Android that can specifically compile xml files

3. After parsing the view, how to get the child view inside, it doesn't work.

This is certainly not that there will be no id index in the layout folder so you can't find it by id. So how to get the subview? Later I found out that someone solved this problem and passed itfindViewWithTag This method can be obtained through the tag configured in the xml view

After the above problem was solved, the xml layout view file was perfectly obtained. It can be dynamically set to activity. Below I paste the source code and friends who need it can refer to it.

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 * Created by yuge on 2017/11/8.
 */

public class AssetsViewHelper {
 private static Context mcontext;
 private static AssetsViewHelper assetsViewHelper;
 /**
   * assets directory prefix
   */
 private static String assetsFile="assets/";
 private AssetsViewHelper(){
 }


 public static AssetsViewHelper width(Context context){
  mcontext=();
  if(assetsViewHelper==null){
    synchronized (){
     if(assetsViewHelper==null){
      assetsViewHelper=new AssetsViewHelper();
     }
    }
  }
  return assetsViewHelper;
 }

 /**
   * Get layout method
   * @param filename
   * @return
   */
 public View getAssetsLayout(String filename) {
  AssetManager am = ().getAssets();
  try {
    XmlResourceParser parser = (assetsFile + "activity_main.xml");
    LayoutInflater inflater = (LayoutInflater) (mcontext.LAYOUT_INFLATER_SERVICE);
    View inflate = (parser, null);
    return inflate;
   } catch (IOException e) {
    ();
    return null;
   }
 }
  /**
    * Get view object according to tag
    * @param viewGroup Parent container is the root layout of activity
    * @param tag
    * @return
    */
 public View getViewByTag(View viewGroup,Object tag){
  return (object);
 }

 /**
   * Methods to get pictures in assets
   * @param fileName
   * @return
   */
  Bitmap getImageFromAssetsFile(String fileName)
 {
  Bitmap image = null;
  AssetManager am = ().getAssets();
  try
  {
   InputStream is = (assetsFile+fileName);
   image = (is);
   ();
  }
  catch (IOException e)
  {
   ();
  }

  return image;

 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.