2013-09-24 55 views
0

创建我有下一个问题的资源库: 我想获得基于Android项目库,并在另一个项目为“外部”库从项目添加

  1. 是否可以使用它?

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

MySituation:

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,我把手动进创建的库。

有人帮我吗?

回答

1

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

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(){ 
    } 
} 
+0

嘿!你偷看我的代码! ))这就是我正在做的事(好吧,更复杂一点)。当我想膨胀未知资源时,问题出现在setContentView()方面。 – chatlanin

+0

所以,我会尝试创建动态布局,而不是静态资源。也许这是唯一的方法。我真的很感谢你的建议。非常感谢! – chatlanin

+0

没问题!随时接受这个答案,如果它帮助你:) – nomachinez