[英]How to put scrollbar in a table layout
I made a program and I made use of table layout as my layout,I want to put scrollbar int it, I used table.setVerticalScrollBarEnabled(true);
我编写了一个程序,并使用表格布局作为布局,我想将滚动条放入其中,我使用了
table.setVerticalScrollBarEnabled(true);。 but I did not work,what is my best remedy?
但是我没有工作,我最好的补救办法是什么? Here are my codes:
这是我的代码:
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);
You have to implement a ScrollView
. 您必须实现
ScrollView
。 You can set 你可以设定
android:scrollbars="@null"
in the XML resource and in that way the scrollbar will be not visible. 在XML资源中,这样滚动条将不可见。
为什么不使用xml将布局设计为tablelayout ... ScrollView必须具有线性布局作为scrollview的子布局..线性布局内部是您的表布局...
在XML中添加一个ScrollView标记,并在其中添加TableLayout ,然后所有表都将使用它来滚动。
you may create it as below: 您可以按以下方式创建它:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.