2011-05-07 150 views
25

我在这里读了这个地方,我完全失去了它,但可以使用一些帮助。动态添加textViews到linearLayout

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

那么,我将如何动态创建一个textView和editText为无数列表是否在数组中?

它是像

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

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

这是正确的?

我感谢您的帮助!

+0

我想你会想一个ListView或类似的东西,而不是在textviews推... – jkhouw1 2011-05-07 00:37:36

+0

我也读过,但我看到有一些问题ems其中人们无法从listView访问editText。如果我确实设法将editText添加到列表中,我该如何动态获取数据? – 2011-05-07 00:39:47

+0

ListView中存在'EditText'的问题。第一个是可以解决的焦点问题(请参阅http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview),但是如果您实际使用'EditText'作为列表项而不是页脚/页眉那么也存在视图回收的问题 - 这意味着您需要在“EditText”文本离开屏幕并被回收之前保存文本。 – 2011-05-07 01:19:59

回答

56

类似下面的应该是你所需要的:

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

酷!请试试看,这也可以用于editText吗? – 2011-05-07 01:20:40

+0

是的,相同的模式适用于'EditText',实际上任何视图类型! – 2011-05-07 01:21:05

+0

约瑟夫,你真棒! – 2011-05-07 01:59:26

3

我认为这将是有用的:

int j = 0; 

context.getSystemService(Context.WINDOW_SERVICE); 
WindowManager manager = (WindowManager) context 
         .getSystemService(Context.WINDOW_SERVICE); 
Display display = manager.getDefaultDisplay(); 

for (int i = 0; i < tabsize; i++) { 
    Tab tab = tabSet.get(i); 
    if (i == selectedTabId) 
     tab.setSelected(true); 
    View view = tab.getView(); 

    TableRow.LayoutParams pCol = new TableRow.LayoutParams(); 
    pCol.width = display.getWidth()/tabSet.size(); 

    rowBottom.addView(view, pCol); 
} 
7
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout2); 

for (int i = 0; i < 5; i++) 
{ 
    TextView tv = new TextView(this); 
    tv.setText("Dynamic TextView" + i); 
    tv.setId(i + 5); 
    ll.addView(tv); 
} 

u can do like this also.. 
+2

如何将'OnClickListeners'绑定到创建的元素? – 2014-06-18 02:45:29

3

使用ArrayList的可以帮助你添加任何数量TextViews的动态。您甚至可能想要从父线性布局中删除特定的TextView。这是一种有效的记忆方式。以下是一个片段。

ArrayList<TextView> mTextViewList = new ArrayList<>(); //empty list of TextViews 

if(condition){ 
    /* can repeat several times*/ 

    //Create a temporary instance which will be added to the list 
    final TextView mTextView = new TextView(this); 

    //Add the instance to the ArrayList 
    mTextViewList.add(mTextView); 

    //Add view to the Parent layout in which you want to add your views 
    mLinearLayout.addView(mTextView); 
} 

//Change the text of 6th(index:5) TextView which was added 
mTextViewList.get(5).setText("My Text"); 

//Remove 2nd(index:1) TextView from the parent LinearLayout 
mLinearLayout.removeView(mTextViewList.get(1)); 
0

因此,可以说你已经创建了一个的LinearLayout .xml文件,像这里面:

<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/linear" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</LinearLayout> 

现在的代码中添加5个textviews动态

LinearLayout linearLayout= (LinearLayout)findViewById(R.id.linear);  //find the linear layout 
    linearLayout.removeAllViews();        //add this too 
    for(int i=0; i<5;i++){   //looping to create 5 textviews 

     TextView textView= new TextView(this);    //dynamically create textview 
     textView.setLayoutParams(new LinearLayout.LayoutParams(   //select linearlayoutparam- set the width & height 
       ViewGroup.LayoutParams.MATCH_PARENT, 48)); 
     textView.setGravity(Gravity.CENTER_VERTICAL);      //set the gravity too 
     textView.setText("Textview: "+i);         //adding text 
     linearLayout.addView(textView);          //inflating :) 
    } 
0

对我来说,这是一个解。

//设置变量

TextView t; 
ArrayList<TextView> textViewArrayList; 
LayoutInflater layoutInflater; 
LinearLayout ll_itensobrigatorios 

//告诉你的onCreate

layoutInflater = getLayoutInflater(); 
createViewItem(new String[]{"Fabio", "Santos", "Programador", "Natal"}); 

//这个创建视图布局

private void createViewItem(String[] nomes) { 
      textViewArrayList = new ArrayList<>(); 

      for(int i = 0; i < nomes.length; i++) { 
       View vll = layoutInflater.inflate(R.layout.nomes_tec_item, ll_itensobrigatorios, false); 
       t = (TextView) vll.findViewById(R.id.txt_tec_item); 
       textViewArrayList.add(t); 
       ll_itensobrigatorios.addView(vll); 
      } 

      for(int i = 0; i < textViewArrayList.size(); i++) { 
       textViewArrayList.get(i).setText((i + 1) + " - " + nomes[i]); 
      } 
}