2012-03-30 125 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 ==>然後去庫==>點擊添加===>選擇你的項目。

相關問題