2013-04-24 72 views
0

我试图在运行时更新/填充xml。 textViews显示正常,但似乎无法在第一个项目后正确定位它们(​​请参阅else语句)。它是因为getId()不被识别或者我完全错误?以编程方式构建xml相对布局

for(int x=1; x<13; x++){ 
    String prompt="PROMPT_"+String.valueOf(x); 
    String promptValue = myTacorCursor.getString(myTacorCursor.getColumnIndex(prompt)); 
    //if not empty draw a row 
    if (!promptValue.equals("")){ 
     //insert new rows into layout 
     RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1); 
     TextView promptLabel = new TextView(this); 
     promptLabel.setTextAppearance(this, android.R.style.TextAppearance_DeviceDefault_Large); 
     promptLabel.setText(myTacorCursor.getString(myTacorCursor.getColumnIndex("PROMPT_"+String.valueOf(x))));     
     promptLabel.setId(1); 
     ((RelativeLayout) myLayout).addView(promptLabel); 

     RelativeLayout.LayoutParams mLayoutParams1=(RelativeLayout.LayoutParams)promptLabel.getLayoutParams(); 
     mLayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     if (i==1){ 
      mLayoutParams1.addRule(RelativeLayout.BELOW,R.id.textView7); 
      Log.w("ID is:", String.valueOf(promptLabel.getId())); 
     } else{     
      mLayoutParams1.addRule(RelativeLayout.BELOW,promptLabel.getId()); 
      Log.w("ID is:", String.valueOf(promptLabel.getId())); 
     } 
    i++; 
    } 
} 

我试图显示:

(textView)LABEL xx R.id.textview7 
<-- here would be the inserted columns --> 
(text view) prompt 1 
(text view) prompt 2 
(text view) prompt 3 
... etc ...' 
+0

这将是很有用的,如果你说你想要在屏幕上显示。 – 2013-04-24 13:14:00

回答

0

动态设置ID是可以的。只是更注意一些,你的代码工作。

  1. 只要您在for循环中,最好只在一个位置增加索引。
  2. 你变了,你需要将其设置回的ViewLayoutParams后:promptLabel.setLayoutParams(layoutParams)

试试这个。它应该工作:

public class MainActivity extends Activity { 

    private static final int BASE_ID = 1; 
    private static final int ITEMS_COUNT = 13; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.root); 

     for (int index = BASE_ID; index < ITEMS_COUNT; index++) { 
      String prompt = "PROMPT_" + String.valueOf(index); 
      // if not empty draw a row 
      // insert new rows into layout 
      TextView promptLabel = new TextView(this); 
      promptLabel.setTextAppearance(this, 
        android.R.style.TextAppearance_DeviceDefault_Large); 
      promptLabel.setText(prompt); 
      promptLabel.setId(index); 
      rootLayout.addView(promptLabel); 

      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) promptLabel 
        .getLayoutParams(); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      if (index == BASE_ID) { 
       layoutParams.addRule(RelativeLayout.BELOW, R.id.top); 
       Log.d("ID is:", String.valueOf(index)); 
      } else { 
       layoutParams.addRule(RelativeLayout.BELOW, index - 1); 
       Log.d("ID is:", String.valueOf(index - 1)); 
      } 
      promptLabel.setLayoutParams(layoutParams); 
     } 
    } 
} 
+0

完美!现在一切都说得通了。 – user2224135 2013-04-26 08:10:37

0

你不需要编程构建整个布局。将其定义在单独的布局xml文件中,然后使用布局充气器来充气。之后将它添加到你想要的地方。

我从未见过id以编程方式分配,但我的建议是你可以像往常一样在xml中定义它们。

PS:Here是一个很好的例子,解释如何使用LayoutInflater

+0

我可以看到你从哪里来,我试图实现它,但我有同样的问题,因为我希望它尽可能动态。问题:如果我将使用LayoutInflater,那么我可以直接覆盖textView的ID,还是必须在xml中创建13个textView实例,因为可能会有1,2或13个提示。 – user2224135 2013-04-24 15:51:35

+0

@ user2224135你为什么瞄准使用ID而不是标签?我知道你可以通过程序控制标签。 – 2013-04-24 16:19:02

+0

这是为了更好的控制和数据处理,因为我有许多可能的布局变化。看到解决问题的riwnodennyk答案。您的建议想法也适用 – user2224135 2013-04-26 08:08:44