我还有一个问题:
我想获得一个基于android项目的库,并在另一个项目中将其用作“外部”库


可能吗?
如果可能-我的主要问题:如何将资源添加到库中?项目属性-> Android->“作为库”仅添加.class文件的方式!


我的情况:

public class ClassForLibrary extends Activity{

      onCreate(){
         setContentView(R.layout.library_activity_layout);
      }

      method1(){
      }

      method2(){
      }
}


因此,我想拥有一个包含两个文件的库:ClassForLibrary.class和library_activity_layout.xml

接下来,我要使用“构建路径”添加该库...

并以以下身份启动应用程序

public class MainActivity extends ClassForLibrary {

    . . . . . . . . . .

}


我已经尝试了很多代码构造,但最接近的情况是

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001.


#0x7f030001是我手动放入创建的库中的library_activity_layout.xml。

有人帮我吗?

最佳答案

您不能在另一个应用程序中访问私有库文件,除非该库本身公开了它们。在您的情况下,您的库需要创建一个函数,让应用程序使用其自己的布局替换它。就像是:

public class ClassForLibrary extends Activity{

  onCreate(){
     setContentView(R.layout.library_activity_layout);
  }

  public overrideContentView(ViewGroup root){
      .... replace root view with the one passed in....
  }

  method1(){
  }

  method2(){
  }
}

10-06 13:41
查看更多