0

我正在尝试在运行时更新/填充 xml。textViews 显示得很好,但似乎无法在第一项之后正确定位它们(​​请参阅 else 语句)。是因为getId()不被认可还是我完全错了?

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 ...'
4

2 回答 2

1

动态设置 id 是可以的。只需多加注意,您的代码就可以工作。

  1. 就您而言for,最好只在一个地方增加索引。
  2. 更改LayoutParams后,View您需要将其重新设置: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);
        }
    }
}
于 2013-04-25T14:27:02.330 回答
0

您不需要以编程方式构建整个布局。在单独的布局xml文件中定义它,然后使用布局充气器对其进行充气。之后将其添加到您想要的位置。

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

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

于 2013-04-24T13:44:48.273 回答