我試圖在運行時更新/填充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 ...'
這將是很有用的,如果你說你想要在屏幕上顯示。 – 2013-04-24 13:14:00