当前位置:操作系统 > 安卓/Android >>

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.在父项目中使用子项目的资源。

 

 

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.貌似这样的应用有很大好处,子项目可以得到充分的利用,大家今后一定会应用到的,谢谢!

 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,