Android Программное добавление более одного TextView в TableRow

У меня есть LinearLayout внутри FrameLayout для вкладок. И я пытаюсь добавить TableRow в LinearLayout в коде.

LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

Это получает мой LinearLayout и создает TableRow в соответствии со спецификациями, которые я хочу. Я знаю, что эта часть работает.

TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");

TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");

Здесь я без проблем делаю два своих TextView.

tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

Здесь я предполагаю, что все идет не так. Что происходит, так это показывает только textTwo. Я не знаю, почему он не показывает их обоих по порядку, как обычный TableRow в XML. И я повторяю, это должно быть сделано в коде. Пожалуйста, помогите, спасибо.


person user2009781    schedule 17.03.2013    source источник


Ответы (2)


Вы импортировали это import android.widget.TableRow.LayoutParams

Ниже код работает для меня

  TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
  TableRow tr = new TableRow(this);
  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  tr.setLayoutParams(lp);

  TextView tvLeft = new TextView(this);
  tvLeft.setLayoutParams(lp);
  tvLeft.setBackgroundColor(Color.WHITE);
  tvLeft.setText("OMG");
  TextView tvCenter = new TextView(this);
  tvCenter.setLayoutParams(lp);
  tvCenter.setBackgroundColor(Color.WHITE);
  tvCenter.setText("It");
  TextView tvRight = new TextView(this);
  tvRight.setLayoutParams(lp);
  tvRight.setBackgroundColor(Color.WHITE);
  tvRight.setText("WORKED!!!");

  tr.addView(tvLeft);
  tr.addView(tvCenter);
  tr.addView(tvRight);

  tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

  tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
person Nirali    schedule 05.06.2013

Я думаю, вы хотите создать свой TextView динамически и должны получить ошибку «removeView () parent». Вот хорошее решение для этого:

TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
     TableRow tableRow = new TableRow(getContext());

     int i = 0;
     for (Skills s : listSkills) {
     TextView textView = new TextView(getContext());
     textView.setText("" + s.getName());

     tableRow.addView(textView);

     if(i > 0) { 
          tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
     }

     tlSkills.addView(tableRow);

     i++;
  }
}
person Paulo Linhares - Packapps    schedule 23.08.2016