将 textView 动态添加到 linearLayout [英] Dynamically add textViews to a linearLayout

查看:25
本文介绍了将 textView 动态添加到 linearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I read this somewhere here and I totally lost it, but could use some assistance.

我的应用程序正在将列名从 sqlite 拉到一个数组中.我想创建一个文本视图并为每个文本编辑文本(通过数组的大小),我记得在某处读过您可以将 textViews 变量名称视为数组,但我不知道现在在哪里.

My app is pulling the column names from sqlite into an array. I want to create a textview and edit text for each one (via the size of the array), and I remember reading somewhere that you can treat the textViews variable names like an array, but I don't know where that is now.

那么我将如何为数组中的许多列表动态创建 textView 和 editText?

So how would I dynamically create a textView and editText for however many listings are in an array?

有点像

TextView tv[] = new TextView()...

for(...){
tv[i]...
}

是这样吗?

感谢您的帮助!

推荐答案

你需要的应该是这样的:

Something like the following should be what you need:

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;
}

这篇关于将 textView 动态添加到 linearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆