在我的Activity中,我必须添加相同TextView的10倍。
是否可以从layout.xml加载textview的定义并以编程方式重复它?
答案 0 :(得分:4)
for(int i=0;i<10;i++){
Textview text = new TextView(this);
mainlayout.add(text);
}
答案 1 :(得分:1)
您可能希望阅读有关重用UI组件的文章:http://developer.android.com/resources/articles/layout-tricks-reuse.html
答案 2 :(得分:1)
public class YourClassName extends Activity
{
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
// set activity layout
setContentView(R.layout.some_activity_layout);
LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.main_layout);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// then see previous answer
// loop n times {
TextView yourTextView = _li.inflate(R.layout.text_view_layout, null);
mainActivityLayout.addView(yourTextView);
// } end loop
}
}