Dynamically add textViews to a linearLayout

后端 未结 6 1356
孤独总比滥情好
孤独总比滥情好 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:04

    You can add TextViews at runtime by following code below:

    LinearLayout lLayout = (LinearLayout) findViewById(R.id.linearlayout2); // Root ViewGroup in which you want to add textviews
    for (int i = 0; i < 5; i++) {
        TextView tv = new TextView(this); // Prepare textview object programmatically
        tv.setText("Dynamic TextView" + i);
        tv.setId(i + 5);
        lLayout.addView(tv); // Add to your ViewGroup using this method
    }
    

提交回复
热议问题