0

タブの目的で FrameLayout 内に LinearLayout があります。そして、コードの LinearLayout に TableRow を追加しようとしています。

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

ここで、2 つの TextView を問題なく作成します。

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

ここで、すべてがうまくいかないと思います。何が起こるかというと、textTwo だけが表示されます。XML での通常の TableRow のように、両方が順番に表示されない理由がわかりません。繰り返しますが、これはコードで行う必要があります。助けてください、ありがとう。

4

2 に答える 2

0

TextView を動的に作成したいので、「removeView () 親」というエラーが発生するはずです。そのための良い解決策は次のとおりです。

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++;
  }
}
于 2016-08-23T15:34:06.827 に答える