1
我具有其中包含報頭(列名稱)的活性的tablelayout,中間佈局(列數據)和頁腳(按鈕)添加垂直和水平滾動。現在我需要一個垂直滾動中間佈局和水平滾動標題和中間佈局。編程在活動
中間佈局的垂直滾動工作正常,但水平滾動不起作用。
我怎麼能在一個活動中以編程方式添加這兩種方式滾動不使用XML?
這是我的代碼到目前爲止。
RelativeLayout rlBody = new RelativeLayout(this);
rlBody.setPadding(0, 0, 0, 0);
RelativeLayout.LayoutParams bodyParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlBody.setLayoutParams(bodyParams);
but_downl = new Button(getApplicationContext());
but_downl.setText("Download");
but_downl.setWidth(width);
RelativeLayout footer = new RelativeLayout(this);
RelativeLayout.LayoutParams footerParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
footer.setLayoutParams(footerParams);
footer.addView(but_downl);
RelativeLayout rlmaintableLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlmaintableLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlmaintableLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlmaintableLayoutParams.addRule(RelativeLayout.ABOVE, footer.getId());
rlmaintableLayout.setLayoutParams(rlmaintableLayoutParams);
TableLayout maintableLayout = new TableLayout(getApplicationContext());
maintableLayout.setVerticalScrollBarEnabled(true);
TextView tvQnr, tvDownLType, tvPublishBy, tvPublishDate, tvMonPlan, tvLang, textViewMsg;
CheckBox chk, chkparent;
View v;
tableRow = new TableRow(getApplicationContext());
v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
chkparent=new CheckBox(this);
tableRow.addView(chkparent);
tvQnr=new TextView(getApplicationContext());
tvQnr.setText("Name");
tvQnr.setTextColor(Color.parseColor("#235C8B"));
tvQnr.setTypeface(null, Typeface.BOLD);
tvQnr.setTextSize(20);
tvQnr.setPadding(20, 20, 30, 20);
tableRow.addView(tvQnr);
tvDownLType=new TextView(getApplicationContext());
tvDownLType.setText("Age");
tvDownLType.setTextColor(Color.parseColor("#235C8B"));
tvDownLType.setTypeface(null, Typeface.BOLD);
tvDownLType.setTextSize(20);
tvDownLType.setPadding(20, 20, 20, 20);
tableRow.addView(tvDownLType);
tvPublishBy=new TextView(getApplicationContext());
tvPublishBy.setText("Gender");
tvPublishBy.setTextColor(Color.parseColor("#235C8B"));
tvPublishBy.setTypeface(null, Typeface.BOLD);
tvPublishBy.setTextSize(20);
tvPublishBy.setPadding(20, 20, 20, 20);
tableRow.addView(tvPublishBy);
tvPublishDate=new TextView(getApplicationContext());
tvPublishDate.setText("Published Date");
tvPublishDate.setTextColor(Color.parseColor("#235C8B"));
tvPublishDate.setTypeface(null, Typeface.BOLD);
tvPublishDate.setTextSize(20);
tvPublishDate.setPadding(20, 20, 20, 20);
tableRow.addView(tvPublishDate);
tvMonPlan=new TextView(getApplicationContext());
tvMonPlan.setText("Roll No");
tvMonPlan.setTextColor(Color.parseColor("#235C8B"));
tvMonPlan.setTypeface(null, Typeface.BOLD);
tvMonPlan.setTextSize(20);
tvMonPlan.setPadding(20, 20, 20, 20);
tableRow.addView(tvMonPlan);
tvLang=new TextView(getApplicationContext());
tvLang.setText("Language");
tvLang.setTextColor(Color.parseColor("#235C8B"));
tvLang.setTypeface(null, Typeface.BOLD);
tvLang.setTextSize(20);
tvLang.setPadding(20, 20, 20, 20);
tableRow.addView(tvLang);
maintableLayout.addView(tableRow);
maintableLayout.addView(v);
RelativeLayout scrollHolder = new RelativeLayout(this);
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
scrollHolder.setLayoutParams(scrollHolderParams);
ScrollView myScrollView = new ScrollView(this);
myScrollView.setEnabled(true);
HorizontalScrollView horscrollview = new HorizontalScrollView(this);
horscrollview.setEnabled(true);
TableLayout tableLayout = new TableLayout(getApplicationContext());
int cnt = 0;
for (int j = 0; j < myFormresult.size(); j++){
tableRow = new TableRow(getApplicationContext());
v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
cnt = cnt + 1;
chk = new CheckBox(this);
tvQnr = new TextView(getApplicationContext());
tvQnr.setTextColor(Color.parseColor("#235C8B"));
tvQnr.setTextSize(15);
tvQnr.setText(myFormresult.get(j).getFormName());
tvQnr.setPadding(25, 10, 20, 10);
tvDownLType = new TextView(getApplicationContext());
tvDownLType.setTextColor(Color.parseColor("#235C8B"));
tvDownLType.setTextSize(15);
tvDownLType.setText(myFormresult.get(j).getVersion());
tvDownLType.setPadding(25, 10, 20, 10);
tvPublishBy = new TextView(getApplicationContext());
tvPublishBy.setTextColor(Color.parseColor("#235C8B"));
tvPublishBy.setTextSize(15);
tvPublishBy.setText(myFormresult.get(j).getPublishedBy());
tvPublishBy.setPadding(25, 10, 20, 10);
tvPublishDate = new TextView(getApplicationContext());
tvPublishDate.setTextColor(Color.parseColor("#235C8B"));
tvPublishDate.setTextSize(15);
tvPublishDate.setText(myFormresult.get(j).getPublishedDate());
tvPublishDate.setPadding(25, 10, 20, 10);
tvMonPlan = new TextView(getApplicationContext());
tvMonPlan.setTextColor(Color.parseColor("#235C8B"));
tvMonPlan.setTextSize(15);
tvMonPlan.setText(myFormresult.get(j).getMonitoringPlan());
tvMonPlan.setPadding(25, 10, 20, 10);
tvLang = new TextView(getApplicationContext());
tvLang.setTextColor(Color.parseColor("#235C8B"));
tvLang.setTextSize(15);
tvLang.setText(myFormresult.get(j).getLanguage());
tvLang.setPadding(25, 10, 20, 10);
tableRow.addView(chk);
tableRow.addView(tvQnr);
tableRow.addView(tvDownLType);
tableRow.addView(tvPublishBy);
tableRow.addView(tvPublishDate);
tableRow.addView(tvMonPlan);
tableRow.addView(tvLang);
tableLayout.addView(tableRow);
tableLayout.addView(v);
tableLayout.addView(vertView);
}
if (cnt == 0) {
textViewMsg = new TextView(getApplicationContext());
textViewMsg.setText("No records found ...");
textViewMsg.setTextColor(Color.parseColor("#235C8B"));
textViewMsg.setTextSize(15);
tableLayout.addView(textViewMsg);
but_downl.setEnabled(false);
}
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
horscrollview.addView(tableLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
myScrollView.addView(horscrollview);
scrollHolder.addView(myScrollView);
maintableLayout.addView(scrollHolder);
rlmaintableLayout.addView(maintableLayout);
rlBody.addView(rlmaintableLayout);
rlBody.addView(footer);
在此先感謝...
你可以發佈你的代碼?你是什麼意思'但水平滾動不起作用。' – InnocentKiller
我已經發布了我的代碼。垂直滾動工作正常,但當我添加水平滾動,中間佈局消失。 – Suniel