0

我在相对布局中动态创建了动态表格布局。我正在动态添加文本视图。我有 9 个字段要显示。如何在其中放置水平和垂直滚动条?

代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        // Show the Up button in the action bar.
        setupActionBar();

        TableLayout tableLayout = new TableLayout(getApplicationContext());
        TableRow tableRow;
            TextView textView;


           ............................
           ............................
           ............................
           for (int i = 0; i < rows; i++) 
            {                               //4 rows
            tableRow = new TableRow(getApplicationContext());

            for (int j = 0; j < columns; j++) 
            {                           //3 columns
                textView = new TextView(getApplicationContext());

                textView.setText(temp[k++]);
                textView.setPadding(15, 15, 15, 15);
                tableRow.addView(textView);

            }

            tableLayout.addView(tableRow);
        }

             setContentView(scrollView);
        setContentView(tableLayout);
4

2 回答 2

1

像这样创建你的 layout.xml 文件

<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res/com.smartcloud.podcast_he"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_iphone" >
        <ScrollView
            android:id="@+id/ScrollView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/headingtitle2" >

        </ScrollView>
 </RelativeLayout>

然后在代码中

setContentView(R.layout.activity_view);
        // Show the Up button in the action bar.
setupActionBar();

ScrollView scrollview = (ScrollView) findViewById(R.id.ScrollView03);

TableLayout tableLayout = new TableLayout(getApplicationContext());
    TableRow tableRow;
        TextView textView;


       ............................
       ............................
       ............................
       for (int i = 0; i < rows; i++) 
        {                               //4 rows
        tableRow = new TableRow(getApplicationContext());

        for (int j = 0; j < columns; j++) 
        {                           //3 columns
            textView = new TextView(getApplicationContext());

            textView.setText(temp[k++]);
            textView.setPadding(15, 15, 15, 15);
            tableRow.addView(textView);

        }

        tableLayout.addView(tableRow);
    }
scrollview.addView(tableLayout );

删除这 2 行

    setContentView(scrollView);
    setContentView(tableLayout);
于 2013-03-13T12:41:48.660 回答
0

使您的顶级布局成为ScrollView

scrollView.addView(tableLayout);
setContentView(scrollView);
于 2013-03-13T12:32:11.760 回答