我们在编程的时候,除了可以利用布局中的TextView来显示数据,同样我们也可以利用对象来动态创建TextView,并展示数据。
1、创建TextView对象
2、添加数据和属性值
3、把TextView设置为布局的子节点
4、如果需要滚动需要使用ScrollView
LinearLayout ll = (LinearLayout) findViewById(R.id.lay);
//把数据显示至屏幕
for (Person p : personList) {
//1.集合中每有一条元素,就new一个textView
TextView tv = new TextView(this);
//2.把信息设置为文本框的内容
tv.setText(p.toString());
tv.setTextSize(20);
//3.把textView设置为线性布局的子节点
ll.addView(tv);
}
布局代码
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<LinearLayout
android:id="@+id/lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>