简体   繁体   English

如何在TableView中为特定行设置背景颜色

[英]How can I set the background color for a specific row in my TableView

I want to set the the color for a row in my TableView but am having some difficulties. 我想在TableView中为行设置颜色,但是遇到了一些困难。 I tried 我试过了

ListView.getChildAt(0).setBackgroudColor() ListView.getChildAt(0).setBackgroudColor()

but it says I have no children. 却说我没有孩子 I do have 5 items but don't know how to say, "Set this item's background color to red" I want to do this programmatically in any method in my code. 我确实有5个项目,但不知道怎么说:“将该项目的背景色设置为红色”,我想用我的代码中的任何方法以编程方式进行此操作。

Any suggestions? 有什么建议么?

This is probably more effort than you'd want to do, but you could make your own custom adapter and set your background for that row in the adapter. 这可能比您想做的事多,但是您可以制作自己的自定义适配器并为适配器中的该行设置背景。

Here is a nice tutorial that does it: 这是一个很好的教程 ,可以做到这一点:

Basically in your adapter, you can override getView 基本上在适配器中,您可以覆盖getView

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      if (position == 0) {
          view.setBackgroundColor(yourColor);
      }
      return view;
    }

One advantage to doing this... you can do further customizations to that row to make it stand out. 这样做的优点之一是...您可以对该行进行进一步的自定义以使其突出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM