开发者

How to display an array of TextViews in android dynamically?

I want to display 26 textviews. How to display an array of TextViews 开发者_开发问答in android dynamically?


Create an xml layout which consist of a scrollview containing a linearlayout with orientation vertical.

In the activity, get the linear layout by using findViewById function.

Run a for loop to create a textview with relevant text and continue calling

addView method on this LinearLayout.


see the bellow code

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    LinearLayout ll=new LinearLayout(this);
    for(int i=0;i<26;i++){
        TextView tv=new TextView(this);
        // or get your TextView from array
        char c=(char)(65+i);
        tv.setText(" "+c);
        ll.addView(tv);
    }
    setContentView(ll);
 }

but this type of design is not good,instead try to use ListView with CustomAdapters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜