无法更改生成的表格行颜色

时间:2014-04-08 11:23:43

标签: android

当我用xml创建表格行时,我可以改变它的颜色。这是我使用的方式:

Resources resource = context.getResources();
row.setBackgroundColor(resource .getColor(R.color.green));

但是当我按代码生成tablerow时它不会改变....

这是代码

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);

有什么问题?如何改变颜色?

1 个答案:

答案 0 :(得分:0)

解决了!!! 问题是在onclick中我应该使用View v和nor row更改颜色。之前它只更改最后一行