2
如何在運行時將新的TextView添加到佈局中?可能嗎?如何在運行時添加TextView?
如何在運行時將新的TextView添加到佈局中?可能嗎?如何在運行時添加TextView?
編程添加TextView
(或一般的View
)到佈局(或一般的ViewGroup
)是可能的,請檢查ViewGroup的public void addView (View child, int index)
方法。
完整的解決方案:
View parent; //parent view where to add
ViewGroup layout=new LinearLayout(context);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tv1=new TextView(context);
tv1.setText("Test1");
TextView tv2=new TextView(context);
tv2.setText("Test2");
layout.addView(tv1);
layout.addView(tv2);
parent.addView(layout);
哪裏聲明 「背景」?它在任何地方初始化 – 2013-11-26 09:00:50