今天在讀源碼的時候發現,一個android項目A作爲另一個android項目B的子項目,B中可以使用A中的類以及方法。自己沒嘗試過,下面就是我實踐記錄。
1.創建子項目A,命名爲MyAndroidLib。
2.將MyAndroidLib項目設置成IsLibaray。同時爲了避免和父項目存在命名衝突,將main.xml和string.xml 分別命名爲lib.xml和libstring.xml.
如下圖:
3.在父項目MyProject中添加子項目作爲libaray。
4.在父項目中使用子項目的資源。
- package com.cczscq.project;
- import com.cczscq.UserTest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class ProjectActivity extends Activity {
- /** Called when the activity is first created. */
- TextView textView1 = null;
- TextView textView2 = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textView1 = (TextView) findViewById(R.id.text1);
- textView2 = (TextView) findViewById(R.id.text2);
- // 引用lib項目中的資源
- UserTest userTest = new UserTest();
- userTest.setUserName("I'm here!");
- textView1.setText(R.string.question);
- textView2.setText(userTest.getUserName());
- }
- }
5.運行結果:
6.貌似這樣的應用有很大好處,子項目可以得到充分的利用,大家今後一定會應用到的,謝謝!
來源:http://blog.csdn.net/look_down/article/details/6625817