Android loads the Xml layout file in the Assets directory

  • 2021-10-27 08:50:23
  • OfStack

Recently, due to the use of dynamic layout in project development, it takes 1 layout file to package sdk and sdk. The small partners who have done sdk development should know that layout files cannot be packaged into jar. Except for aar, of course. Because the project is still using jar package, so how to solve the layout file is a problem, 1 the way to think of is to send layout file to customers. However, this method is obviously not suitable. Later, it was discovered that Android actually provides a method to load xml layout files, that is, using inflate (XmlPullParser parser, ViewGroup root), a large number of articles were found on the Internet, among which two articles were found that briefly described the next parsing process, but there were still several problems in the use process:

1 How to get XmlPullParser object

It is not difficult to get this object. We can easily get XmlResourceParser openXmlResourceParser through AssetsManger (String fileName)
But note that there is a problem here, that is, filename should be prefixed with "assets\", otherwise FileNotFound will be reported as abnormal

2 found that the xml layout file could not be parsed

The openxmlresourceparser method reports an error. Why? Find the data because this method can only parse the compiled xml file, so what the compiled xml file is the generated apk decompressed xml is compiled. Therefore, xml that we put in assets should be compiled files. There is no other tool for Android to compile xml files

3 Analyzed how to get the child view inside view. I can't pass id

That's for sure not in the layout folder there will be no id index so you can't use id to get find. Then how to get the sub-view, and later found that someone solved this problem by findViewWithTag, which can be obtained by tag configured under xml view

After solving the above problems, we got the layout of xml perfectly, and the view file can be dynamically set to activity. Below, I put the source code up, and friends who need it can refer to it.


import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;

/**
 * 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=context.getApplicationContext();
  if(assetsViewHelper==null){
    synchronized (AssetsViewHelper.class){
     if(assetsViewHelper==null){
      assetsViewHelper=new AssetsViewHelper();
     }
    }
  }
  return assetsViewHelper;
 }

 /**
  *  Get layout Method 
  * @param filename
  * @return
  */
 public View getAssetsLayout(String filename) {
  AssetManager am = mcontext.getResources().getAssets();
  try {
    XmlResourceParser parser = am.openXmlResourceParser(assetsFile + "activity_main.xml");
    LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(mcontext.LAYOUT_INFLATER_SERVICE);
    View inflate = inflater.inflate(parser, null);
    return inflate;
   } catch (IOException e) {
    e.printStackTrace();
    return null;
   }
 }
  /**
   *  According to  tag  Get  view  Object 
   * @param viewGroup  The parent container is activity Root layout of 
   * @param tag
   * @return
   */
 public View getViewByTag(View viewGroup,Object tag){
  return viewGroup.findViewWithTag(object);
 }

 /**
  *  Get assets  Method of picture in 
  * @param fileName
  * @return
  */
  Bitmap getImageFromAssetsFile(String fileName)
 {
  Bitmap image = null;
  AssetManager am = mcontext.getResources().getAssets();
  try
  {
   InputStream is = am.open(assetsFile+fileName);
   image = BitmapFactory.decodeStream(is);
   is.close();
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }

  return image;

 }
}

Related articles: