【问题标题】:Adding Scrollbar to Dynamic Table-View in Relative Layout在相对布局中将滚动条添加到动态表格视图
【发布时间】:2013-03-13 12:30:07
【问题描述】:

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

【问题讨论】:

    标签: android tableview android-scrollbar


    【解决方案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);
    

    【讨论】:

    • 太棒了。它适用于垂直滚动条。我怎样才能把水平的?
    【解决方案2】:

    将您的顶级布局设为ScrollView

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

    【讨论】:

    • 我是 Android 新手。其实我已经试过了。它不起作用。你能告诉我我应该把它放在我的代码中的什么地方吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多