简体   繁体   中英

android dynamic screen field placement

I'm building an app that needs to create the screen on the fly. I want to create a series of identical line with 4 Text fields. I'm missing something because all the fields are listed under the previous field.

My code loop:

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

    for(int i = 0; i < 10; i++) 
    {
        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);
        ll.addView(tvItem);

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

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

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

    }

Each view added within the loop must be next to each other. before going to the next line.

What am I missing?

Create inner LinearLayout for each row, like this:

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM