android libaray的創建和使用

今天在讀源碼的時候發現,一個android項目A作爲另一個android項目B的子項目,B中可以使用A中的類以及方法。自己沒嘗試過,下面就是我實踐記錄。

1.創建子項目A,命名爲MyAndroidLib。

2.將MyAndroidLib項目設置成IsLibaray。同時爲了避免和父項目存在命名衝突,將main.xml和string.xml 分別命名爲lib.xml和libstring.xml.

如下圖:



3.在父項目MyProject中添加子項目作爲libaray。


4.在父項目中使用子項目的資源。


[java] view plaincopy
  1. package com.cczscq.project;  
  2.   
  3. import com.cczscq.UserTest;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. public class ProjectActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     TextView textView1 = null;  
  12.     TextView textView2 = null;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         textView1 = (TextView) findViewById(R.id.text1);  
  19.         textView2 = (TextView) findViewById(R.id.text2);  
  20.           
  21.         // 引用lib項目中的資源  
  22.         UserTest userTest = new UserTest();  
  23.         userTest.setUserName("I'm here!");  
  24.           
  25.         textView1.setText(R.string.question);  
  26.         textView2.setText(userTest.getUserName());  
  27.          
  28.     }  
  29. }  

5.運行結果:

6.貌似這樣的應用有很大好處,子項目可以得到充分的利用,大家今後一定會應用到的,謝謝!

來源:http://blog.csdn.net/look_down/article/details/6625817


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章