Android Jar Resource

  jar引用的资源文件必须放在assets文件夹中,并且布局文件和Drawable必须是编译过的格式,否则布局文件无法解析,一般的Drawable尺寸不正确,Nine-Patch无法拉伸。

  抽取布局文件的方法如下,其中fileName必须包含assets/路径:

    public static View extractView(Context context, String fileName, ViewGroup root) throws Exception {
            XmlResourceParser parser = context.getAssets().openXmlResourceParser(fileName);
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(parser, root);

    }

    注意,如果root为null,那么该布局文件的根元素属性会被忽略,比如固定的长和宽等,解决的一个办法是在外层再嵌套一个ViewGroup。

  抽取Drawable的方法是,其中fileName不必包含assets/路径:

    public static Drawable extractDrawable(Context context, String fileName) throws Exception {

      InputStream inputStream = context.getAssets().open(fileName);
      TypedValue value = new TypedValue();

      /** 传入TypeValue,指定资源的像素密度是基于320*480屏幕的 */
      value.density = 160; 

      /** 传入Resources,以获取目标屏幕像素密度 */ 

      Drawable drawable = Drawable.createFromResourceStream(context.getResources(), value, inputStream, fileName);
      inputStream.close();
      return drawable;
    }

  StateListDrawable添加状态时,enabled状态必须放在最后,值为false的状态只需取状态常量的相反数即可。

  ProgressBar在xml文件中如果声明了进度,则在代码中更改ProgressDrawable无效,必须去除前述声明。

posted on 2011-03-03 13:57  爱吃草莓的维尼  阅读(897)  评论(0编辑  收藏  举报

导航