[英]how to use a project as a library api in another project in android?
I have a splitview project using fragment in android, where i used some method to generateviews, define width of the splits and a method to set the background colour, here is my java code: 我有一个在Android中使用fragment的splitview项目,在该项目中,我使用了一些方法来生成视图,定义拆分的宽度以及设置背景颜色的方法,这是我的Java代码:
public class SplitviewActivity extends FragmentActivity implements
Left.OnButtonClickListener {
LinearLayout LEFT, RIGHT, leftfragmentln, rightfragmentln;
Right rightFr;
Left leftFr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//reduced the Gray Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
// initializes the objects
initializer();
// sets the size of the two sides, give the ration here
setSize(20, 80);
// sets the colour of the two sides
setColor(Color.BLUE, Color.RED);
}
public void initializer() {
LEFT = (LinearLayout) findViewById(R.id.lnLeft);
RIGHT = (LinearLayout) findViewById(R.id.lnRight);
leftfragmentln = (LinearLayout) findViewById(R.id.lnLeftFragment);
rightfragmentln = (LinearLayout) findViewById(R.id.lnRightFragment);
rightFr = (Right) getSupportFragmentManager().findFragmentById(
R.id.view_right);
leftFr = (Left) getSupportFragmentManager().findFragmentById(
R.id.view_left);
}
public void setColor(int leftColor, int rightColor) {
// for landscape mode
if ((rightFr != null) && rightFr.isInLayout() && (leftFr != null)
&& leftFr.isInLayout()) {
LEFT.setBackgroundColor(leftColor);
RIGHT.setBackgroundColor(rightColor);
} else {
// for portrait mode
RIGHT.setBackgroundColor(rightColor);
}
}
public void setSize(float leftsize, float rightsize) {
// for landscape mode
if ((rightFr != null) && rightFr.isInLayout() && (leftFr != null)
&& leftFr.isInLayout()) {
android.widget.LinearLayout.LayoutParams par1 = (LayoutParams) LEFT
.getLayoutParams();
android.widget.LinearLayout.LayoutParams par2 = (LayoutParams) RIGHT
.getLayoutParams();
par1.weight = rightsize;
par2.weight = leftsize;
}
}
// a method to add view in a blank xml programatically
public void setView(View leftView, View rightView) {
if ((rightFr != null) && rightFr.isInLayout() && (leftFr != null)
&& leftFr.isInLayout()) {
leftfragmentln.addView(leftView);
rightfragmentln.addView(rightView);
} else {
rightfragmentln.addView(rightView);
}
}
@Override
public void onClickButton(String s) {
if ((rightFr != null) && rightFr.isInLayout()) {
rightFr.setText(s);
} else {
Intent intent = new Intent(this, RightActivity.class);
intent.putExtra("value", s);
startActivity(intent);
}
}
}
now I want to use this code as a library in another project and just want to call these methods to generate views, define width and setbackground colour. 现在,我想将此代码用作另一个项目中的库,只想调用这些方法来生成视图,定义宽度和setbackground颜色。 I have tried in different ways, but nothing was able to solve my problem. 我已经尝试了不同的方法,但是没有任何方法能够解决我的问题。 Can any one help me with a simple example? 有人可以帮我举一个简单的例子吗?
I currently do this with a plain java project. 我目前使用纯Java项目来执行此操作。 To include I configured in the build path 包括我在构建路径中配置的
a) Project: 一个专案:
just add your project 只需添加您的项目
b) Order and Export b)订购和出口
select (check) your project 选择(检查)您的项目
右键单击项目窗格中的项目->属性-> Java构建路径->项目或库之一!
Create your new android project in Eclipse then go to your existing project and 在Eclipse中创建新的android项目,然后转到现有项目并
Properties => Android => is library project => tick this 属性=> Android =>是库项目=>勾选此
Then in the new project go to 然后在新项目中转到
Properties => Android => add library project then select your lib project from the list 属性=> Android =>添加库项目,然后从列表中选择您的lib项目
You must leave the lib project open. 您必须将lib项目保持打开状态。 This same method is used by Facebook and actionbar Sherlock. Facebook和动作栏Sherlock使用相同的方法。
右键单击另一个项目->属性->选择android ==>的过滤器类型,然后转到库==>单击添加===>选择项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.