2012-05-04 68 views
0

我有一个LinearLayout,我在其中动态创建了一定数量的TextView。
有时候屏幕上会显示更多的TextView。将滚动条添加到动态视图

如何向此视图添加滚动条,以便用户可以上下滚动并查看所有TextView?

这里是我的代码部分:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout); 
for (int n = 0; n < (numberOfPlayers*(numberOfPlayers-1)/2); n++) { 
    TextView tv = new TextView(this); 
    tv.setText(gamelist[n][0] + " - " + gamelist[n][1]); 
    layout.addView(tv); 
} 

回答

1

包括您的LinearLayout在滚动型

<ScrollView 
    android:id="@+id/scroll" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"> 

    <LinearLayout ... /> 

</ScrollView> 

注意,滚动型只能有一个视图孩子

1

包装你LinearLayoutScrollView

在这种情况下,您可以考虑使用ListView(教程here)。