我有一个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);
}
答案 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)。