2014-03-14 36 views
0

我有一個onClickListener,其中我嘗試將兩個視圖動態添加到現有ViewGroup。我只是想在另一個的右邊添加一個,但是不管我做什麼,它們都是正確的,它們的左邊對齊。其他方面的佈局是服從的。例如,我可以指定一個寬度爲MATCH_PARENT,視圖按照這種方式呈現。另外,我正在編程模仿我如何爲XML中的另一個ViewGroup指定佈局,並且XML指定的佈局能夠正常工作。這裏是我的代碼:Android:以編程方式添加視圖並且不遵守佈局

Editable nodeName = nodeSelectView.getText(); 
View insertPoint = findViewById(R.id.insertionPoint); 

//the two views to be added dynamically 
EditText nodeView = new EditText(ManageDomainsActivity.this); 
Button nodeButton = new Button(ManageDomainsActivity.this); 

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
lp.addRule(RelativeLayout.LEFT_OF, nodeButton.getId()); 
nodeView.setLayoutParams(lp); 
nodeView.setGravity(Gravity.LEFT); 
nodeView.setText(nodeName.toString()); 

lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
lp.addRule(RelativeLayout.RIGHT_OF, nodeView.getId()); 
lp.addRule(RelativeLayout.ALIGN_BASELINE, nodeView.getId()); 
lp.addRule(RelativeLayout.ALIGN_BOTTOM, nodeView.getId()); 
nodeButton.setLayoutParams(lp); 
nodeButton.setText("Kill"); 
nodeButton.setGravity(Gravity.CENTER); 

((ViewGroup) insertPoint).addView(nodeView, 0, 
     new ViewGroup.LayoutParams(
     ViewGroup.LayoutParams.WRAP_CONTENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT)); 
((ViewGroup) insertPoint).addView(nodeButton, 1, 
     new ViewGroup.LayoutParams(
     ViewGroup.LayoutParams.WRAP_CONTENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT)); 

回答

0

的問題是,我是在經過了addView LayoutParameters()被重寫我已經在各自的意見設置的。它也因爲循環依賴而失敗,因爲這兩個視圖分別被定義在彼此的右側和左側。這對我來說似乎並不循環,並且它在XML中正常工作,但它在動態佈局分配中引發了異常。下面是工作代碼:

Editable nodeName = nodeSelectView.getText(); 
View insertPoint = findViewById(R.id.insertionPoint); 

EditText nodeView = new EditText(ManageDomainsActivity.this); 
nodeView.setId(1); 
Button nodeButton = new Button(ManageDomainsActivity.this); 
nodeButton.setId(2); 

RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
lpView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
nodeView.setLayoutParams(lpView); 
nodeView.setGravity(Gravity.LEFT); 
nodeView.setText(nodeName.toString()); 

RelativeLayout.LayoutParams lpButton = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
lpButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
lpButton.addRule(RelativeLayout.RIGHT_OF, nodeView.getId()); 
lpButton.addRule(RelativeLayout.ALIGN_BASELINE, nodeView.getId()); 
lpButton.addRule(RelativeLayout.ALIGN_BOTTOM, nodeView.getId()); 
nodeButton.setLayoutParams(lpButton); 
nodeButton.setText("Kill"); 
nodeButton.setGravity(Gravity.CENTER); 

((ViewGroup) insertPoint).addView(nodeView, lpView); 
((ViewGroup) insertPoint).addView(nodeButton, lpButton); 
1

你需要設置的要創建的視圖的ID的編程方式,因爲在一個視圖中使用的getId()沒有一個id返回一個NO_ID常數不RelativeLayout的規則工作。以編程方式創建的視圖不需要全局唯一的ID(僅在視圖組中是唯一的),因此您可以將它們設置爲1,2,3,...等。

相關問題