android libaray的建立和使用

來源:互聯網
上載者:User

 

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.