android - 将 textView 动态添加到 linearLayout

标签 android textview android-edittext add dynamic

我在这里的某个地方读到了这篇文章,我完全失去了它,但可以使用一些帮助。

我的应用程序正在将列名从 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/

相关文章:

android - Firebase 通知 - 无效的注册 token ,请检查 Android 中的 token 格式

android - 使用复合可绘制对象在 TextView 中设置实际文本填充

android - 如何将录制的语音编码为 ogg vorbis?

android - Android EditText 的控制键盘类型(仅限数字)

android - 应用程序更新的推送通知

Android-Textview 在拖动操作时消失

ios - 如何将 TextView 嵌入具有多个部分的 TableView 单元格内?

Android:EditText 没有被椭圆化

android - 如何在默认键盘上放置一个按钮?我想要一个按钮,当按下它时,它有机会进行另一项 Activity

android - 编辑文本行间距额外问题