Android 加载Assets目录中Xml布局文件

  最近由于项目开发使用到了动态布局,因为打包sdk ,sdk 这块activity 需要一些layout 文件 。而做过sdk 开发的小伙伴应该知道,layout 文件是不能打包到jar 中的。当然了aar 除外。由于项目使用的还是jar包,所以怎么解决layout文件是个问题,一开始想到的办法就是把layout 文件发给客户。但是这种方法显然不太合适后来就发现了Android 其实提供了一个方法可以加载xml布局文件
   就是使用inflate(XmlPullParser parser, ViewGroup root)这个方法,网上找了大批的文章,其中还是找到了两篇简单描写了下这个解析的过程但是在使用过程中还是出现了几个问题
  1 如何拿到XmlPullParser 对象
拿到这个对象倒是不难我们通过 AssetsManger 就可以轻易获取XmlResourceParser openXmlResourceParser(String fileName)
但是注意这里有个问题就是filename 要加上”assets\”前缀不然会报 FileNotFound异常
  2 发现解析不了xml 布局文件
openxmlresourceparser 方法报错,为什么呢。查到资料是因为这个方法只能解析编译后的xml文件,那么什么事编译后的xml文件,就是生成的apk 解压后 拿到的xml就是编译后的。所以我们放在assets 中的xml 都要是编译后的文件。目前还没有找到Android有别的工具可以专门编译xml 文件
  3 解析到了view 如何拿到里面的子view 通过id 不行啊
这是肯定的不是在layout文件夹下的不会有id 索引所以你不能通过id 来find 。那么如何拿到子view ,后来发现了有人解决这个问题就是通过findViewWithTag 这个方法可以通过xml view 下配置的tag 来获取
  以上问题解决后就完美拿到了xml 的布局view 文件 可以动态设置给activity了。下面我把源码贴上来需要的朋友可以参考下。

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 目录前缀
     */
    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;
    }

    /**
     * 获取layout方法
     * @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;
            }
    }
        /**
         * 根据 tag 获取 view 对象
         * @param viewGroup 父容器也就是activity的根布局
         * @param tag
         * @return
         */
    public  View getViewByTag(View viewGroup,Object tag){
        return viewGroup.findViewWithTag(object);
    }

    /**
     * 获取assets 中图片的方法
     * @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;

    }
}

猜你喜欢

转载自blog.csdn.net/ImTryCatchException/article/details/78486675
今日推荐