2012-03-30 56 views
0

我使用的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颜色。我尝试过不同的方式,但没有什么能够解决我的问题。任何人都可以用一个简单的例子来帮助我吗?

回答

0

我目前使用普通的java项目来做到这一点。 要包括我在构建路径配置

一) 项目:

只需添加项目

B) 订单和出口

选择(检查)项目

+0

您的suggession工作正常,我可以从我的图书馆项目创建对象,但是我想知道如何在我的新项目中调用这些方法?我打电话,但它抛出异常 – 2012-03-30 08:04:55

0

右键单击项目窗格中的项目 - >属性 - > Java构建路径 - >其中一个项目或库!

0

在Eclipse中创建新的Android项目,然后去你的现有项目,

属性=>安卓=>是库项目=>勾选此

然后在新的项目中去

Properties => Android =>添加库项目,然后从列表中选择您的lib项目

您必须打开lib项目。 Facebook和操作栏Sherlock使用同样的方法。

0

右键单击你的另一个项目 - >属性 - >选择过滤器的类型为android ==>然后去库==>点击添加===>选择你的项目。

相关问题