在相对布局中将滚动条添加到动态表视图

时间:2013-03-13 12:30:08

标签: android tableview android-scrollbar

我在Relative-Layout中动态创建了动态表格布局。我正在动态添加文本视图。我有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);

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

将您的顶级布局设为ScrollView

scrollView.addView(tableLayout);
setContentView(scrollView);