如何更改动态创建表格行的背景颜色

时间:2017-08-16 07:37:07

标签: java android

我已动态创建表格行并添加了带表格布局的视图 现在我想更改所选行的背景颜色和特定文本视图,当用户选择另一行时,前一行颜色应该被禁用。 提前致谢。 下面是我的代码。

    for(int s=0;s<lstFilteredTradeOrder.size();s++)
    {
        final TableRow tableRow         = new TableRow(OrderbookFragment.this.getActivity());
        tableRow.setId(s);

        tableRow.setClickable(true);
        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT) );
        tableRow.setWeightSum(1);
        final View rowView;
        rowView = OrderbookFragment.this.getActivity().getLayoutInflater().inflate(R.layout.custom_order_book, null);
        TextView textName                   = (TextView)rowView.findViewById(R.id.txt_name);
        TextView textLastUpdated            = (TextView)rowView.findViewById(R.id.txt_lastupdated);
        TextView textAction                 = (TextView)rowView.findViewById(R.id.txt_action);
        TextView textOrdPrice               = (TextView)rowView.findViewById(R.id.txt_ordprice);
        TextView textOrdQty                 = (TextView)rowView.findViewById(R.id.txt_ordqty);
        TextView textStatus                 = (TextView)rowView.findViewById(R.id.txt_status);
        final ImageView imageView                =(ImageView)rowView.findViewById(R.id.image_arrow);

        textName.setText(lstFilteredTradeOrder.get(s).getstockCodeName());
        textLastUpdated.setText(FormatUtil.formatDateString(String.valueOf(lstFilteredTradeOrder.get(s).getLastUpdate()),
                "yyyyMMddHHmmss", "dd/MM/yyyy HH:mm:ss"));
        textAction.setText(lstFilteredTradeOrder.get(s).getAction());
        textOrdPrice.setText(String.format("%.3f", lstFilteredTradeOrder.get(s).getPrice()));
        textOrdQty.setText(FormatUtil.formatOpDouble(lstFilteredTradeOrder.get(s).getQuantity()));
        textStatus.setText(lstFilteredTradeOrder.get(s).getStatusText());


        tableRow.addView(rowView);

        tableRow.setOnClickListener(new View.OnClickListener() {
              Boolean color=true;

            public void onClick(View view) {

                 // here would like to change the background color.
                TradeOrder selectOrd= lstFilteredTradeOrder.get(tableRow.getId());
                 constructOrderbookOnclick(selectOrd);
            }
        });


        tblOrderbookDetails.addView(tableRow, new TableLayout.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    }

1 个答案:

答案 0 :(得分:0)

只需使用以下代码

data_Row_tv.setBackgroundColor(ContextCompat.getColor(context, R.color.your_color));