Dynamically add textViews to a linearLayout

后端 未结 6 1366
孤独总比滥情好
孤独总比滥情好 2020-11-29 00:23

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

My app is pulling the column names from sqlite into an array. I want to create a tex

6条回答
  •  余生分开走
    2020-11-29 01:15

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

提交回复
热议问题