【发布时间】:2014-04-08 20:06:48
【问题描述】:
当我使用 xml 创建表格行时,我可以在单击时更改其颜色。这是我使用的方式:
Resources resource = context.getResources();
row.setBackgroundColor(resource .getColor(R.color.green));
但是当我通过代码生成表格时,它并没有改变......
这是代码
row = new TableRow(dialog.getContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setBackgroundColor(Color.WHITE);
row.setPadding(dpToPx(5), dpToPx(5), dpToPx(5), dpToPx(5));
text = new TextView(dialog.getContext());
text.setBackgroundColor(Color.BLACK);
text.setText(patients.get(1).getPatientInternalId());
text.setTextAppearance(this, android.R.style.TextAppearance_Large);
text.setId(i);
TableRow.LayoutParams l1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
l1.setMargins(dpToPx(2), 0, 0, 0);
text.setLayoutParams(l1);
row.addView(text);
text = new TextView(dialog.getContext());
text.setBackgroundColor(Color.BLACK);
text.setText(patients.get(1).getPatientStudyId());
text.setTextAppearance(this, android.R.style.TextAppearance_Large);
TableRow.LayoutParams l2 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
l2.setMargins(dpToPx(5), 0, 0, 0);
text.setLayoutParams(l2);
row.addView(text);
text = new TextView(dialog.getContext());
text.setBackgroundColor(Color.BLACK);
text.setText(patients.get(1).getPatientInitials());
text.setTextAppearance(this, android.R.style.TextAppearance_Large);
LinearLayout.LayoutParams l3 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
l3.setMargins(dpToPx(5), 0, 0, 0);
text.setLayoutParams(l3);
row.addView(text);
text = new TextView(dialog.getContext());
text.setBackgroundColor(Color.BLACK);
text.setText(patients.get(1).getEndTime());
text.setTextAppearance(this, android.R.style.TextAppearance_Large);
LinearLayout.LayoutParams l4 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
l4.setMargins(dpToPx(5), 0, 0, 0);
text.setLayoutParams(l4);
row.addView(text);
row.setClickable(true);
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Resources resource = context.getResources();
row.setBackgroundColor(resource .getColor(R.color.green));
String a = ((TextView) row.getChildAt(0)).getText().toString();
}
});
table.addView(row);
有什么问题?如何改变颜色?
【问题讨论】:
-
检查你的第三行 -> row.setBackgroundColor(Color.WHITE);尝试删除这个。
-
然后呢?我想通过点击改变颜色
-
你应该试试 row.setBackgroundColor(Color.parseColor("#00ff00"));而是 row.setBackgroundColor(resource .getColor(R.color.green));
标签: android