【问题标题】:How to change background color dynamically created table row如何更改动态创建的表格行的背景颜色
【发布时间】:2017-08-16 07:37:06
【问题描述】:

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

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

【问题讨论】:

    标签: java android


    【解决方案1】:

    使用下面的代码

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

    【讨论】:

    • 谢谢,但它不能正常工作,因为当我选择另一行时,前一行也突出显示,我想一次突出显示一行。
    猜你喜欢
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多