如何将滚动条放在表格布局中

时间:2013-11-12 05:42:32

标签: android scroll android-tablelayout

我制作了一个程序,我使用表格布局作为我的布局,我想把滚动条放在它中,我用 table.setVerticalScrollBarEnabled(true); 但是我没有工作,是什么我最好的补救措施? 这是我的代码:

 TableLayout table = new TableLayout(getApplicationContext());
            table.setVerticalScrollBarEnabled(true);// i used this but it did not work
            table.setPadding(10, 10, 10, 10);


            TableRow tableRow = new TableRow (getApplicationContext());             

            TextView txt = new TextView (getApplicationContext());
            TextView txt2 = new TextView (getApplicationContext());
            TextView txt3 = new TextView (getApplicationContext());
            TextView txt4 = new TextView (getApplicationContext());
            TextView txt5 = new TextView (getApplicationContext());
            TextView txt6 = new TextView (getApplicationContext());

            tableRow.addView(txt);
            tableRow.addView(txt2);
            tableRow.addView(txt3);
            tableRow.addView(txt4);
            tableRow.addView(txt5);
            tableRow.addView(txt6);



            tableRow.setBackgroundColor(Color.GRAY);

            txt.setText("Question  ");
            txt2.setText("Excellent   ");
            txt3.setText("Best     ");
            txt4.setText("Better   ");
            txt5.setText("Good     ");
            txt6.setText("Poor     ");

            txt.setTextColor(Color.BLACK);
            txt2.setTextColor(Color.BLACK);
            txt3.setTextColor(Color.BLACK);
            txt4.setTextColor(Color.BLACK);
            txt5.setTextColor(Color.BLACK);
            txt6.setTextColor(Color.BLACK);


            table.addView(tableRow);

            int j=0;
            for(j = 1; j<=count; j++){
                Random rnd = new Random(); 
                int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 


                tableRow = new TableRow (getApplicationContext());
                TextView name = new TextView (getApplicationContext());
                EditText et2 = new EditText (getApplicationContext());
                EditText et3 = new EditText (getApplicationContext());
                EditText et4 = new EditText (getApplicationContext());
                EditText et5 = new EditText (getApplicationContext());
                EditText et6 = new EditText (getApplicationContext());



                et2.setBackgroundColor(color);
                et3.setBackgroundColor(color);
                et4.setBackgroundColor(color);
                et5.setBackgroundColor(color);
                et6.setBackgroundColor(color);

                name.setText("Q#"+Integer.toString(j));





                tableRow.addView(name);
                tableRow.addView(et2);
                tableRow.addView(et3);
                tableRow.addView(et4);
                tableRow.addView(et5);
                tableRow.addView(et6);
                table.addView(tableRow);

            }
            TableRow tableRow1 = new TableRow (getApplicationContext());

            Button showtable = new Button(getApplicationContext());
            tableRow1.addView(showtable);
            showtable.setText("Show Table");
            showtable.setTextSize(8);

            table.addView(tableRow1);



            setContentView(table);

4 个答案:

答案 0 :(得分:0)

您必须实施ScrollView。你可以设置

android:scrollbars="@null"

在XML资源中,以这种方式滚动条将不可见。

答案 1 :(得分:0)

为什么不使用xml将你的布局设计为tablelayout ... ScrollView必须有一个线性布局作为scrollview的子布局......在线性布局中是你的表格布局......

答案 2 :(得分:0)

在XML中添加 ScrollView 标记并在其中添加 TableLayout ,然后所有表格都将使用它滚动。

答案 3 :(得分:0)

您可以按以下方式创建:

ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));

table.addView(tableRow1);
scroll.addView(table);
   setContentView(scroll);