[轉載]android工程中引入另一個工程中的資源

來源:互聯網
上載者:User

標籤:

原文地址:android工程中引入另一個工程中的資源 87fayuan

在項目中可能遇到這樣的問題:項目過大,於是細分為N個子模組來做,每個模組都是不同的工程。涉及到activity傳資料時,可以用intent等方法來解決。但是如果涉及到要共用資源,而又不能像傳統java程式那樣打成jar包,比如程式中有大量自訂view,而這些自訂view都引用了的一些資源檔時,就可以用這個方法。

 

工程一:MyViews

代碼如下:

public class MyTextView extends TextView{

 public MyTextView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  this.setBackgroundColor(Color.BLUE);
  this.setText(context.getResources().getString(R.string.test_view));
 }

}

對工程一,右鍵-->properties,勾選Is Library,確定即可。



工程二:TestActivity

首先對工程二,右鍵-->properties-->android-->Add-->MyViews,然後確定即可

代碼如下:

public class TestActivity extends Activity {
   
 private MyTextView mtv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mtv=new MyTextView(this);
        setContentView(mtv);
    }
}

運行工程二,可發現成功調用了工程一中的MyTextView

[轉載]android工程中引入另一個工程中的資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.