2010-10-18 84 views
2

我在動態添加文本框到我的視圖時遇到問題。更具體地說,文本框加法器的作品,但我試圖在它下面移動的按鈕不。原始視圖是以下超鏈接中的第一張圖片。在相對佈局中動態添加文本框的問題

按下+/-按鈕後,它應該在第二個文本框和決定按鈕之間添加一個文本框,然後向下移動+/-按鈕,使其位於新框的旁邊。取而代之的是,第二張照片發生了:

http://i.stack.imgur.com/mzBL3.png

我的代碼如下所示:

EditText textBox2 = (EditText) findViewById(R.id.box2); 

      RelativeLayout rel = (RelativeLayout) findViewById(R.id.mainlayout); 

      Context context = getApplicationContext(); 
      EditText newText = new EditText(context); 

      newText.setId(numBoxes); 
      numBoxes++; 

      LayoutParams p = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
      p.addRule(RelativeLayout.BELOW, bottomView.getId()); 
      p.addRule(RelativeLayout.ALIGN_RIGHT, bottomView.getId()); 
      newText.setWidth(220); 
      newText.setHeight(LayoutParams.WRAP_CONTENT); 
      newText.setLayoutParams(p); 
      rel.addView(newText); 
      bottomView = newText; 
      ((TextView) bottomView).setText((CharSequence)bottomView.getTag()); 

      LayoutParams b = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
      Button goButton = (Button)findViewById(R.id.decide); 
      b = (LayoutParams) goButton.getLayoutParams(); 
      b.addRule(RelativeLayout.BELOW, bottomView.getId()); 
      goButton.setLayoutParams(b); 

      Button addButton = (Button)findViewById(R.id.addsub); 
      b = (LayoutParams) addButton.getLayoutParams(); 
      b.addRule(RelativeLayout.RIGHT_OF, textBox2.getId()); 
      b.addRule(RelativeLayout.ALIGN_TOP, bottomView.getId()); 
      addButton.setLayoutParams(b); 

Numboxes只是一個計數器,以便我可以據此跟蹤的箱子,並將它們命名,並bottomView是隻是最底部的文本框。

我搜遍了所有的android開發頁面,並嘗試創建新的佈局參數。我也嘗試用文本框替換決定按鈕,bu發生同樣的問題。請幫忙。

回答

2

對於那些關心,我是這個開發團隊的一部分,我們解決了我們的問題。

這是問題的概念。

LayoutParams p = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

在我們的佈局中,我們使用的是相對佈局,因此該行應爲:

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

分配不兼容的佈局參數會導致問題。