標籤:
原文地址:android工程中引入另一個工程中的資源
87fayuan
在項目中可能遇到這樣的問題:項目過大,於是細分為N個子模組來做,每個模組都是不同的工程。涉及到activity傳資料時,可以用intent等方法來解決。但是如果涉及到要共用資源,而又不能像傳統java程式那樣打成jar包,比如程式中有大量自訂view,而這些自訂view都引用了的一些資源檔時,就可以用這個方法。
工程一:MyViews
代碼如下:
public class MyTextView extends TextView{
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.setBackgroundColor(Color.BLUE);
this.setText(context.getResources().getString(R.string.test_view));
}
}
對工程一,右鍵-->properties,勾選Is Library,確定即可。
工程二:TestActivity
首先對工程二,右鍵-->properties-->android-->Add-->MyViews,然後確定即可
代碼如下:
public class TestActivity extends Activity {
private MyTextView mtv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mtv=new MyTextView(this);
setContentView(mtv);
}
}
運行工程二,可發現成功調用了工程一中的MyTextView
[轉載]android工程中引入另一個工程中的資源