将滚动条添加到动态视图

时间:2012-05-04 13:38:15

标签: android scrollbar

我有一个LinearLayout,我动态地创建了一定数量的TextViews 有时候,屏幕上会有更多的TextViews。

如何向此视图添加滚动条,以便用户可以向上和向下滚动并查看所有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);
}

2 个答案:

答案 0 :(得分:2)

将您的linearLayout包含在ScrollView

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

    <LinearLayout ... />

</ScrollView>

请注意,ScrollView只能有一个视图子项

答案 1 :(得分:1)

LinearLayout换入ScrollView

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