android动态屏幕字段放置

时间:2016-01-05 19:49:40

标签: android dynamic screen

我正在构建一个需要动态创建屏幕的应用。我想用4个文本字段创建一系列相同的行。我遗漏了一些东西,因为所有字段都列在前一个字段下。

我的代码循环:

struct product
{
    string name;
    int weight;
    double price;
};

product *p1 = new product();
(*p1).name = "orange"; // This is an explicit dereferencing
p1->name = "orange";   // This is also an explicit dereferencing

product *p2 = &p1;
p2.name = "apple"; // This is an implicit dereferencing

循环中添加的每个视图必须彼此相邻。在去下一行之前。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

为每一行创建内部LinearLayout,如下所示:

LinearLayout outer = new LinearLayout(this);
outer.setOrientation(LinearLayout.VERTICAL);

LayoutParams lParm = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

for(int i = 0; i < 10; i++) 
{

    LinearLayout inner = new LinearLayout(this);
    inner.setOrientation(LinearLayout.HORIZONTAL);

    TextView tvItem = new TextView(this);
    tvItem.setLayoutParams(lParm);
    tvItem.setText("Item #"+i);
    tvItem.setBackgroundColor(0xff66ff66);
    tvItem.setEms(7);
    tvItem.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
    inner.addView(tvItem);

    EditText etPrep = new EditText(this);
    etPrep.setLayoutParams(lParm);
    etPrep.setText("0");
    etPrep.setEms(2);
    inner.addView(etPrep);

    EditText etOpen = new EditText(this);
    etOpen.setLayoutParams(lParm);
    etOpen.setText("0");
    etOpen.setEms(2);
    inner.addView(etOpen);

    EditText etCase = new EditText(this);
    etCase.setLayoutParams(lParm);
    etCase.setText("0");
    etCase.setEms(2);
    inner.addView(etCase);

    outer.addView(inner);

}