转自 http://blog.chinaunix.net/uid-23392298-id-3322769.html
来源:互联网 发布:网络课程录制 编辑:程序博客网 时间:2024/06/06 00:11
创建library供多个工程共享代码、资源是非常常见的需求,网上这种资料非常少,基本上都是讲创建java工程,然后export,这种方式缺点非常多,大家可以自己google一下。
本文着重介绍如何创建Android library,并且在工程中使用此library提供的资源,具体步骤如下:
1. 创建一个Android工程,命名为MyLib
2. 进入工程设置选中Is Library
3. 创建另一个Android工程,命名为MyProj
4. 进入工程设置,添加MyLib
5. 在MyProj的AndroidManifest.xml中加入对library中activity的引用
<activity android:name="net.devdiv.mylib.MyLib" />
6. 由于编译后library中的资源和引用它的project资源是合并在一起的,为了避免重名问题,需要对library中资源进行重命名
1). 把main.xml改为mylib.xml,同时修改MyLib.java代码setContentView(R.layout.mylib);
2). strings.xml修改为
<?xml version="1.0" encoding="utf-8"?>
< resources>
< string name="mylibhello">String fetched from lib!</string>
< string name="mylib_app_name">MyLib</string>
< /resources>
7. 在MyProj中引用MyLib的资源
package net.devdiv.myproj;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import net.devdiv.mylib.*;
import android.content.Intent;
public class MyProj extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.myprojtext);
tv.setText(R.string.mylibhello);
Intent it = new Intent(this, MyLib.class);
startActivity(it);
}
}
个人觉得应注意:自己定义的library用的权限或是声明,在要引用的工程中要做补充
- 转自 http://blog.chinaunix.net/uid-23392298-id-3322769.html
- curl 转自 http://blog.chinaunix.net/uid-22655387-id-3283161.html
- 一堂课玩转rpm包的制作(转载自http://blog.chinaunix.net/uid-23069658-id-3944462.html)
- http://blog.chinaunix.net/uid-22666718-id-1771703.html
- http://blog.chinaunix.net/uid-20322254-id-145835.html
- http://blog.chinaunix.net/uid-25547034-id-3155778.html
- http://blog.chinaunix.net/uid-25750954-id-2956084.html
- http://blog.chinaunix.net/uid-22342564-id-3053393.html
- http://blog.chinaunix.net/uid-25082381-id-3242162.html
- http://blog.chinaunix.net/uid-25737580-id-3182286.html
- http://blog.chinaunix.net/uid-25835268-id-3055356.html--makefile
- http://blog.chinaunix.net/uid-8874157-id-2012678.html
- http://blog.chinaunix.net/uid-21289517-id-3081793.html
- http://blog.chinaunix.net/uid-20543672-id-3244832.html
- http://blog.chinaunix.net/uid-21222282-id-3244532.html
- http://blog.chinaunix.net/uid-21501855-id-4490453.html
- http://blog.chinaunix.net/uid-20484604-id-1941290.html
- http://blog.chinaunix.net/uid-20569459-id-335214.html
- UIResponder 详解
- Linux的IO复用
- JMeter压力测试案例大全之(ftp服务器和sftp)
- andriod驱动之旅-a31s芯片-android环境
- Effective Java中文版(第2版)
- 转自 http://blog.chinaunix.net/uid-23392298-id-3322769.html
- ubuntu 13.10安装ns2.35详解
- 论程序界面的简洁性
- FFmpeg解码H264及swscale缩放详解
- Thread 中各方法的使用
- SQL 连接查询
- [学习笔记]request.getAttribute与request.getParameter的区别
- 第六十四天~第六十七天(项目)
- sql server网页项目发布到生产环境和开发环境不一致的情况