我在这里的某个地方读到了这篇文章,我完全失去了它,但可以使用一些帮助。
我的应用程序正在将列名从 sqlite 拉到一个数组中。我想创建一个 TextView 并为每个文本编辑文本(通过数组的大小),我记得在某处读过您可以将 textViews 变量名称视为数组,但我不知道现在在哪里。
那么我将如何为数组中的许多列表动态创建 textView 和 editText?
有点像
TextView tv[] = new TextView()...
for(...){
tv[i]...
}
是这样吗?
感谢您的帮助!
最佳答案
您需要的是以下内容:
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
关于android - 将 textView 动态添加到 linearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918320/