android誇項目調用

來源:互聯網
上載者:User

標籤:

將工程A做成android library project。

設定工程A,右鍵->Properties->Android,將Is library項選中,然後Apply。設定工程B,右鍵->Properties->Android,在Library中,點擊Add按鈕,將A工程加入,然後Apply。此時在B中就引入了A中的資源和代碼,這些資源和代碼都可以直接調用。需要注意的是,因為A已經不再是一個完整的Android應用,而是一個類庫工程,所以有一些內容還需要在B中配置一下。比如A中有lib庫引用,則B中也要加入該lib庫;比如A中的AndroidManifest.xml檔案的內容,在B的AndroidManifest.xml檔案中也要相應加上。。。

如果不需要引用A工程的資源檔,同樣只需得到jar檔案,設定工程A,右鍵->Properties->Android,將Is library項選中,然後Apply。在A工程的bin目錄下能得到一個jar檔案,可以copy到B工程中的libs目錄下直接引用。 例:

 

package com.example.titlebar;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隱藏原來的標題        setContentView(R.layout.activity_main);        }}

 

package com.example.titlebar;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class FActivity extends LinearLayout {

public FActivity(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.biaoti, FActivity.this);
// TODO Auto-generated constructor stub
Button backButton = (Button) findViewById(R.id.img);
Button edButton = (Button) findViewById(R.id.img1);
backButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Activity) getContext()).finish();// 而getContext()得到的是this,就相當於this.finish(),
// 其實一般我們在Activity裡直接finish()是一種簡寫
}
});

edButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "edit a ad", Toast.LENGTH_SHORT)
.show();
}
});
}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.titlebar.MainActivity" >    <!-- <include layout="@layout/biaoti" /> -->    <com.example.titlebar.FActivity        android:layout_width="match_parent"        android:layout_height="match_parent" >    </com.example.titlebar.FActivity></RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="#000000"    android:baselineAligned="true" >    <Button        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="5dip"        android:background="@drawable/blue_selector"        android:text="Back" />    <TextView        android:id="@+id/text"        android:layout_width="0dp"        android:layout_height="50dp"        android:layout_gravity="center"        android:layout_weight="1"        android:background="@drawable/ic_dog"        android:gravity="left|center"        android:text="Hello"        android:textSize="25sp" />    <Button        android:id="@+id/img1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="5dip"        android:background="@drawable/blue_selector"        android:text="Edit" /></LinearLayout>

右擊項目點擊Proterties,然後點擊Android,勾選IsLibrary.

點擊apply  

然後新鍵一個項目Test,同樣右擊項目點擊Proterties,然後點擊Android,點擊Add,此時會出現已個TitleBar;然後apply。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.test.MainActivity" >     <com.example.titlebar.FActivity        android:layout_width="match_parent"        android:layout_height="match_parent" >    </com.example.titlebar.FActivity></RelativeLayout>
package com.example.test;import android.support.v7.app.ActionBarActivity;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.Window;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

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.