我使用的Android,在这里我使用了一些方法来generateviews片段的SPLITVIEW项目,定义拆分和设置背景颜色的方法的宽度,这里是我的Java代码:如何在android中的另一个项目中使用项目作为库api?
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);
}
}
}
现在我想要将此代码用作另一个项目中的库,并且只想调用这些方法来生成视图,请定义宽度和setbackground颜色。我尝试过不同的方式,但没有什么能够解决我的问题。任何人都可以用一个简单的例子来帮助我吗?
您的suggession工作正常,我可以从我的图书馆项目创建对象,但是我想知道如何在我的新项目中调用这些方法?我打电话,但它抛出异常 – 2012-03-30 08:04:55