我为一个活动创建了一个布局文件。在这个布局中,我创建了一个带有 textview 和 edittext 的 LinearLayout。现在我想创建额外的LinearLayouts,它们看起来和包含与我原来的LinearLayout完全相同的视图,但文本不同。我还想在运行期间以编程方式执行此操作,因为这些 LinearLayout 的数量在运行之间会有所不同。我读过一些关于充气机的文章,但我对它们的了解还不够,无法使用它们。
我在想这样的事情,显然代码是错误的,但希望你明白我想要做什么:
LinearLayout llMain = (LinearLayout)findViewById(R.id.mainLayout);
LinearLayout llToCopy = (LinearLayout)findViewById(R.id.linearLayoutToCopy);
for(int player = 0; player < size; player++)
{
LinearLayout llCopy = llToCopy.clone();
TextView tv = (TextView)llCopy.getChildAt(0);
tv.setText(players.get(player).getName());
llMain.addView(llCopy);
}