0

我做了一个程序,我用表格布局作为我的布局,我想把滚动条放进去,我用了 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

4 回答 4

0

你必须实现一个ScrollView. 你可以设置

android:scrollbars="@null"

在 XML 资源中,这样滚动条将不可见。

于 2013-11-12T05:44:28.890 回答
0

为什么不使用 xml 将您的布局设计为 tablelayout...ScrollView 必须具有线性布局作为 scrollview 的子布局..并且在线性布局内是您的表格布局...

于 2013-11-12T05:54:46.237 回答
0

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

于 2013-11-12T05:55:05.113 回答
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);
于 2013-11-12T05:58:24.907 回答