【发布时间】:2014-05-29 01:58:26
【问题描述】:
我需要在布局中添加一些字符串,但我需要将每个字符串添加到新行(作为列表)中,并在布局超出垂直尺寸时使布局可滚动。这些字符串的数量是在运行时定义的,所以我这样做:
ScrollView scrollView = new ScrollView(context);
LinearLayout scrollViewLayout = new LinearLayout(context);
scrollViewLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv2;
for(int i = 0; i < count; i++) { //count is defined at runtime when the
//array strings[] is created
//and defines the amount of array's values
tv2 = new TextView(context);
tv2.setLayoutParams(layoutParams);
tv2.setText(strings[i]);
scrollViewLayout.addView(tv2);
}
scrollView.addView(scrollViewLayout);
但我不认为在循环中实例化对象并添加这样的字符串是可以接受的,此外,由于 ScrollView 对象,我收到日志消息“GC_FOR_ALLOC...”和“Grow heap...”所以我认为我应该以另一种更合适的方式执行此操作。请解释我如何正确地将字符串添加到布局并使其可滚动。提前致谢!
【问题讨论】:
标签: android list layout textview scrollview