android libaray的創建和使用 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

今天在讀源碼的時候發現,一個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.貌似這樣的應用有很大好處,子項目可以得到充分的利用,大傢今後一定會應用到的,謝謝!

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *