【问题标题】:can not change generated table row color无法更改生成的表格行颜色
【发布时间】: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


【解决方案1】:

解决了!!! 问题是在onclick中我应该改变颜色usnig View v和nor row.Before它只改变最后一行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    相关资源
    最近更新 更多