我在 stackoverflow.com 上看过几次不可滚动的 ScrollView,但没有一个建议的解决方案对我有用。我希望有人能帮帮忙。
我有一个包含 TableLayout 的 ScrollView。TableLayout 是空的,但我以编程方式插入行和列。我猜我需要告诉 ScrollView TableLayout 已更改/更新,并且 ScrollView 需要重新计算是否需要启用滚动。
我试图使 ScrollView 和 TableLayout 都无效,我尝试了 requestLayout(),但似乎什么也没做。我错过了什么?
这是我的布局 XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="horizontal|vertical" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/maintable" >
</TableLayout>
</ScrollView>
这是我的 Java 代码。基本上,此代码为每个玩家添加一行,并在每个玩家旁边添加 25 个按钮。按钮在那里,但它们超出了屏幕边界。
// Get the TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int player = 0; player < players.length; player++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+player);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to add the player name
TextView labelTV = new TextView(this);
labelTV.setId(200+player);
labelTV.setText(players[player]);
//labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// numShots = 25
for (int shot = 0; shot < numShots; shot++)
{
Button shotButton = new Button(this);
shotButton.setId((player * 100) + shot);
shotButton.setText(Integer.toString(shot));
tr.addView(shotButton);
}
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Add the TableRow to the TableLayout
//};
}