Динамическая настройка ширины строки в TableLayout

#java #android #tablelayout

#Ява #Android #таблица

Вопрос:

Я пытаюсь заполнить таблицу из Json. У меня проблема с установкой веса строк:

Я заполняю таблицу на Java:

  private void initTableByGroup(Stands s, TableLayout groupA) {  TableRow tbrow0 = new TableRow(this.getContext());  tbrow0.setLayoutParams(new TableLayout.LayoutParams(  TableLayout.LayoutParams.MATCH_PARENT,  TableLayout.LayoutParams.MATCH_PARENT, 1.0f));  TextView tv0 = new TextView(this.getContext());  tv0.setText("");  tv0.setTextColor(Color.BLACK);  tv0.setTypeface(null, Typeface.BOLD);  tv0.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv0);  TextView tv1 = new TextView(this.getContext());  tv1.setText("Team");  tv1.setTextColor(Color.BLACK);  tv1.setTypeface(null, Typeface.BOLD);  tv1.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv1);  TextView tv2 = new TextView(this.getContext());  tv2.setText("P");  tv2.setTextColor(Color.BLACK);  tv2.setTypeface(null, Typeface.BOLD);  tv2.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv2);  TextView tv3 = new TextView(this.getContext());  tv3.setText("W");  tv3.setTextColor(Color.BLACK);  tv3.setTypeface(null, Typeface.BOLD);  tv3.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv3);  TextView tv4 = new TextView(this.getContext());  tv4.setText("L");  tv4.setTextColor(Color.BLACK);  tv4.setTypeface(null, Typeface.BOLD);  tv4.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv4);  TextView tv5 = new TextView(this.getContext());  tv5.setText("D");  tv5.setTextColor(Color.BLACK);  tv5.setTypeface(null, Typeface.BOLD);  tv5.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv5);  TextView tv6 = new TextView(this.getContext());  tv6.setText("Dif");  tv6.setTextColor(Color.BLACK);  tv6.setTypeface(null, Typeface.BOLD);  tv6.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv6);  TextView tv7 = new TextView(this.getContext());  tv7.setText("Pts");  tv7.setTextColor(Color.BLACK);  tv7.setTypeface(null, Typeface.BOLD);  tv7.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow0.addView(tv7);  groupA.addView(tbrow0);  for (TeamRank rank : s.getRank()) {  TableRow tbrow = new TableRow(this.getContext());  TextView t1v = new TextView(this.getContext());  t1v.setText(String.valueOf(rank.getRank()));  t1v.setTextColor(Color.BLACK);  t1v.setGravity(Gravity.CENTER);  t1v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t1v);  TextView t2v = new TextView(this.getContext());  t2v.setText(rank.getTeam());  t2v.setTextColor(Color.BLACK);  t2v.setGravity(Gravity.LEFT);  t2v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t2v);  TextView t3v = new TextView(this.getContext());  t3v.setText(String.valueOf(rank.getPlayed()));  t3v.setTextColor(Color.BLACK);  t3v.setGravity(Gravity.CENTER);  t3v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t3v);  TextView t4v = new TextView(this.getContext());  t4v.setText(String.valueOf(rank.getWins()));  t4v.setTextColor(Color.BLACK);  t4v.setGravity(Gravity.CENTER);  t4v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t4v);  TextView t5v = new TextView(this.getContext());  t5v.setText(String.valueOf(rank.getLosses()));  t5v.setTextColor(Color.BLACK);  t5v.setGravity(Gravity.CENTER);  t5v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t5v);  TextView t6v = new TextView(this.getContext());  t6v.setText(String.valueOf(rank.getDraws()));  t6v.setTextColor(Color.BLACK);  t6v.setGravity(Gravity.CENTER);  t6v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t6v);  TextView t7v = new TextView(this.getContext());  t7v.setText(String.valueOf(rank.getDiff()));  t7v.setTextColor(Color.BLACK);  t7v.setGravity(Gravity.CENTER);  t7v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t7v);  TextView t8v = new TextView(this.getContext());  t8v.setText(String.valueOf(rank.getPoints()));  t8v.setTextColor(Color.BLACK);  t8v.setGravity(Gravity.CENTER);  t8v.setTypeface(null, Typeface.BOLD);  t8v.setLayoutParams(new TableRow.LayoutParams(  TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));  tbrow.addView(t8v);  groupA.addView(tbrow);  }  }   

Но я получаю это:

введите описание изображения здесь

Я хотел бы динамически задать ширину каждого столбца (например: позиция = 10% экрана, название команды 30%, ecc ecc). Таким образом, таблица заполнит весь экран, что-то вроде этого (пожалуйста, демонстрация сделана с помощью pait :D): введите описание изображения здесь

Что я могу сделать?

Спасибо!