How can we create dynamic textview?

前端 未结 6 1677
故里飘歌
故里飘歌 2021-01-21 20:13

how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 20:59

    This is the code to create TextView Dynamically

    LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);
    
    for (int i = 0; i < 3; i++) {
    
    TextView dynamicTextView = new TextView(this);
    dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            dynamicTextView.setText("NewYork");
            layout.addView(tstate);
    
    }
    

提交回复
热议问题