android编程时布局文件,图片资源等都是放在同一个文件夹下,这样照成一个问题就是我们想重用UI布局文件和图片时就还需要其分离这些资料,相信大部分android程序员都遇到过这样的问题,其痛苦程度不亚于世纪末日赶不上诺亚方舟。

今天我用apkplug框架实现将不同的资源放在不同的插件apk包中,然后通过插件间类查找的方式实现插件机布局文件共享。不说废话了!

一 新建一个插件myBundle1由它提供布局文件供myBundle插件调用

结合上一篇文章本章我再建一个插件工程myBundle1新增实现3个java类分别是

BundleContextFactory.java 这个类的唯一功能就是存储插件启动时获取的BundleContext,该类中有我们需要的android.content.Context

  1. public class BundleContextFactory implements BundleInstance{
  2. private static BundleContextFactory _instance=null;
  3. private BundleContext mcontext = null;
  4. synchronized public static BundleContextFactory getInstance(){
  5. if(_instance==null){
  6. _instance=new BundleContextFactory();
  7. }
  8. return _instance;
  9. }
  10. private BundleContextFactory(){
  11. }
  12. public BundleContext getBundleContext() {
  13. // TODO Auto-generated method stub
  14. return this.mcontext;
  15. }
  16. public void setBundleContext(BundleContext arg0) {
  17. // TODO Auto-generated method stub
  18. this.mcontext = arg0;
  19. }
  20. }

TBSurfaceView.java 一个SurfaceView 用于演示View

myLayout.java 继承RelativeLayout,通过它插件myBundle就可以获取插件myBundle1的布局文件了

  1. public class myLayout extends RelativeLayout{
  2. public myLayout(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. //获取插件启动时得到的Context
  5. Context m=BundleContextFactory.getInstance().getBundleContext().getBundleContext();
  6. LayoutInflater mInflater=LayoutInflater.from(context);
  7. mInflater = mInflater.cloneInContext(m);
  8. mInflater.inflate(R.layout.main11, this, true);
  9. }
  10. }

布局文件 main11.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextView
  11. android:id="@+id/tt"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="我是插件myBundle1" />
  15. <com.example.mybundle1.TBSurfaceView
  16. android:layout_below="@+id/tt"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. />
  20. </RelativeLayout>

最后插件myBundle1文件目录为

二 修改插件myBundle布局文件activity_main.xml添加View com.example.mybundle1.myLayout

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextView
  11. android:id="@+id/tt"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="hello_world 我是myBundle的Activity" />
  15. <com.example.mybundle1.myLayout
  16. android:layout_below="@+id/tt"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:id="@+id/myLayout1"/>
  20. <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:layout_alignBottom="@+id/myLayout1" android:layout_centerHorizontal="true"/>
  21. </RelativeLayout>

三 将两个编译好的插件添加到主应用的assets文件夹中并在PropertyInstance接口的public String[] AutoStart()中添加两个插件自动启动

  1. public String[] AutoStart() {
  2. File f0=null,f1=null;
  3. try {
  4. InputStream in=context.getAssets().open("myBundle.apk");
  5. f0=new File(context.getFilesDir(),"myBundle.apk");
  6. if(!f0.exists())
  7. copy(in, f0);
  8. } catch (IOException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. try {
  13. InputStream in=context.getAssets().open("myBundle1.apk");
  14. f1=new File(context.getFilesDir(),"myBundle1.apk");
  15. if(!f1.exists())
  16. copy(in, f1);
  17. } catch (IOException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. return new String[]{"file:"+f0.getAbsolutePath(),"file:"+f1.getAbsolutePath()};
  22. }

最后启动应用查看效果如图

最后给出源码

注意:1.以上需要注意的问题的是需要引出的类都应该在plugin.xml文件中添加Export-Package="com.example.mybundle1"  这样插件间才能找的到(下一章会实现另一种方式插件间交换类)

2.apkplug官网为:www.apkplug.com

最新文章

  1. Shader实例:序列帧动画
  2. 【Ckediter】
  3. [.NET] SQL数据分页查询
  4. 注解与反射 ---Spring与Mybatis等框架的实现原理
  5. VIM 技巧 (一)全文统一添加
  6. php运行步骤解析
  7. swift 遍历
  8. 数据结构练习 00-自测4. Have Fun with Numbers (20)
  9. HTML5 Security Cheatsheet使用说明
  10. Divide Two Integers —— LeetCode
  11. Enthought科学计算,数据分析
  12. Android JNI的使用浅析
  13. jupyter 安装、配置及使用笔记
  14. 输入正整数n,求各位数字和
  15. zabbix items
  16. jenkins借助winscp传本地文件到远程服务器上
  17. Windows的空格预览神器 | QuickLook
  18. Jmeter(三十六)纵横并发、限制QPS
  19. 洛谷P4198 楼房重建(线段树)
  20. Java学习--javabean

热门文章

  1. **json_encode:让Json更懂中文(JSON_UNESCAPED_UNICODE)
  2. [YY题]HDOJ5288 OO’s Sequence
  3. spring webservice 开发demo (实现基本的CRUD 数据库采用H2)
  4. lintcode : 二叉树的层次遍历
  5. Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)
  6. C#基础练习(使用委托窗体传值)
  7. ubuntu 乱码 改为英文
  8. POJ 2065 SETI(高斯消元)
  9. Android开发之获取系统管理权限,即DevicePolicyManager和DeviceAdminReceiver的使用
  10. hdu 2986 Ballot evaluation (模拟)