Dynamically add textViews to a linearLayout

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

    So lets say you have created a linearlayout inside a .xml file like this:

    
    
    

    now the code to add 5 textviews dynamically

      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 :)
        }
    

提交回复
热议问题