Программное добавление макета таблицы в линейный макет

Я в основном хочу что-то вроде:

  TextView
  Button
  Button
  .
  .
  .
  TextView   TextView
  SeekBar
  Button

Я должен сделать это программно. Теперь у меня все работает, за исключением того факта, что TextViews над SeekBar не отображаются.

Соответствующий код, выполняющий эту работу,

//Setting the linear layout
    ll =  new LinearLayout(getApplicationContext());                //a linear layout encompasses all the other elements
    ll.setOrientation(LinearLayout.VERTICAL);   //orientation has to be vertical
    ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));     //the layout occupies the complete screen

    //Set the TextView for the Question
    tv1 = new TextView(getApplicationContext());
    tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    //sets the dimensions of the textView
    ll.addView(tv1);                            //add the TextView to the LinearLayout

    //Set the buttons (?)
    b = new Button [MAX_OPTIONS];               
    for(int i=0;i<MAX_OPTIONS;i++){
        b[i] = new Button(getApplicationContext());     //set the context for each button
        b[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        ll.addView(b[i]);
    }

    //Set the Table Layout
    tl = new TableLayout(getApplicationContext());
    tl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    tl.setColumnStretchable(0, true);
    tl.setColumnStretchable(1, true);

    //Set the Table Row
    tr = new TableRow(getApplicationContext());
    tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    tr.setPadding(dipConvert(5), dipConvert(5), dipConvert(5), dipConvert(5));

    //Set the two TextViews
    tv2 = new TextView(getApplicationContext());
    tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tv2.setGravity(Gravity.LEFT);               //set the gravity of this TextView to left
    tv3 = new TextView(getApplicationContext());
    tv3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tv3.setGravity(Gravity.RIGHT);
    tv3.setPadding(0, 0, dipConvert(15), 0);    //set the padding for this to be displayed(the number 15 was emperically determined)

    //Add the Textview to table row
    tr.addView(tv2);        tr.addView(tv3);

    //Add the TableRow to the Table Layout
    tl.addView(tr);

    //Add the TableLayout to the LinearLayout
    ll.addView(tl);

    //Set the Seekbar
    sb1 = new SeekBar(getApplicationContext());
    sb1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    sb1.setMax(100);
    sb1.setMinimumWidth(250);
    ll.addView(sb1);

    //Set the Seekbar Button
    sb1_b = new Button(getApplicationContext());
    sb1_b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    ll.addView(sb1_b);
            setContentView(ll);

Я не могу понять, что происходит. Дисплей, который я получаю, выглядит примерно так

   TextView
   Button
   Button
   .
   .
   .

   SeekBar
   Button

Было бы здорово, если бы кто-то мог указать мне правильное направление.

Спасибо!


person sshasan    schedule 13.06.2012    source источник


Ответы (1)


Вам нужен параметр TableRow.layout в дочерних элементах элементов tableRow. Это было хорошо объяснено здесь. Пожалуйста, обратитесь к нему и исправьте его. Это очень простая икота, которую можно легко проигнорировать.

Ваш текст должен быть

tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

и аналогично для тв3.

person gkris    schedule 14.06.2012
comment
работал отлично для меня! Благодарность! не могли бы вы объяснить, ЗАЧЕМ нам нужно это сделать? Я имею в виду, что мне кажется, что то, что я делал, было хорошо (что, очевидно, не так). Это меня как-то смутило. - person sshasan; 14.06.2012
comment
Здорово. поздравляю. Это связано с тем, что Android различает параметры макета таблицы и строки таблицы. Таким образом, ОС Android должна передать правильный параметр, чтобы понять, что нужно добавить строку. - person gkris; 14.06.2012