由于以编程方式插入数据,ScrollView无法滚动?

时间:2012-10-16 17:26:22

标签: android scrollview scrollable

我在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
            //};
    }

1 个答案:

答案 0 :(得分:1)

我觉得你需要横向滚动? ScrollView只进行垂直滚动,您需要查看HorizontalScrollView