我已经编写了这个方法来动态地在表格布局中添加按钮 我试图在创建按钮时为每个按钮设置高度和宽度,但是有些它是如何工作的
private TableRow.LayoutParams rowParams;
rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rowParams.setMargins(5, 5, 5, 5);
在上面的代码我正在设置行参数
public void addButton_with_filter(final Test test,
TableRow.LayoutParams rowParams, Activity context) {
ArrayList<Integer> filteredquestionno;
TableLayout tl = new TableLayout(context);
LinearLayout filterlayout = (LinearLayout) findViewById(R.id.filterlayout);
filterlayout.removeAllViewsInLayout();
int j = 0;
int k = 0;
filteredquestionno = getLayoutFilteredQuestionNumber(test
.getQuestions());
int size = filteredquestionno.size();
if (size == 0) {
Toast.makeText(this,
"NO QUSTION IS AVAILABLE OF THIS FILTER TYPE !",
Toast.LENGTH_LONG).show();
}
int norows = (size / 5) + 1;
{
for (int i = 0; i < norows; i++) {
TableRow currentRow = new TableRow(FilterActivity.this);
while (j < 5 && k < size) {
j++;
Button qindex = new Button(FilterActivity.this);
qindex.setWidth(5);/*width*/
qindex.setHeight(5);/*height*/
if (submited == true) {
if (test.getQuestionAtIndex(filteredquestionno.get(k))
.isCorrectlyAnswered()) {
qindex.setBackgroundResource(R.drawable.correct_answer);
} else if (test.getQuestionAtIndex(
filteredquestionno.get(k)).isAttempted()) {
qindex.setBackgroundResource(R.drawable.wrong_answer);
} else if (test.getQuestionAtIndex(
filteredquestionno.get(k)).isSkipped()) {
qindex.setBackgroundResource(R.drawable.skipped_answer);
} else if (!test.getQuestionAtIndex(
filteredquestionno.get(k)).isAttempted()) {
qindex.setBackgroundResource(R.drawable.unattempted_question);
}
} else if (submited == false) {
if (test.getQuestionAtIndex(filteredquestionno.get(k))
.isAttempted()) {
qindex.setBackgroundResource(R.drawable.wrong_answer);
} else if (test.getQuestionAtIndex(
filteredquestionno.get(k)).isSkipped()) {
qindex.setBackgroundResource(R.drawable.skipped_answer);
} else if (!test.getQuestionAtIndex(
filteredquestionno.get(k)).isAttempted()) {
qindex.setBackgroundResource(R.drawable.unattempted_question);
}
}
qindex.setText(" " + (filteredquestionno.get(k) + 1));
k++;
qindex.setId(k);
qindex.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
String text = (String) b.getText();
String t = text.trim();
int questionindex = Integer.parseInt(t);
if (type == 0) {
test.setLastQuestionAttempted(questionindex - 1);
FilterActivity.this.finish();
} else if (type == 1) {
test.setLastQuestionAttempted(questionindex - 1);
FilterActivity.this.finish();
}
}
});
currentRow.addView(qindex, rowParams);
}
j = 0;
tl.addView(currentRow);
}
filterlayout.addView(tl);
}
}
答案 0 :(得分:1)
而不是这样做:
Button qindex = new Button(FilterActivity.this);
qindex.setWidth(5);/*width*/
qindex.setHeight(5);/*
将布局参数设置为按钮:
LayoutParams lp= new LayaoutParams(5,5); // width,height
qindex.setLayoutParams(lp);
如果你得到classCastException,请尝试:
TableLayout.LayoutParams lp= new TableLayout.LayaoutParams(5,5); // width,height
qindex.setLayoutParams(lp);