以编程方式构建xml相对布局



我试图在运行时更新/填充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 ...'

可以动态设置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);
        }
    }
}

您不需要以编程方式构建整个布局。在单独的布局xml文件中定义它,然后使用布局膨胀器来膨胀它。然后在你想要的地方添加它。

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

PS:这里有一个很好的例子来解释如何使用LayoutInflater

最新更新