首页 > 代码库 > android夸项目调用
android夸项目调用
将工程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文件中也要相应加上。。。
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夸项目调用