【问题标题】:Color with different color in list view列表视图中不同颜色的颜色
【发布时间】:2013-07-14 18:37:29
【问题描述】:

我有一个简单的列表视图,我必须用不同的颜色为每个替代行着色。

是否可以用不同的颜色为listView着色。

【问题讨论】:

    标签: android listview android-custom-view


    【解决方案1】:

    是的。假设 ArrayAdapter,你将不得不做这样的事情:

    public View getView(int position, View convertView, ViewGroup parent) {
    
       ...
    
       if (position % 2 == 0) { // Even numbered row
          // set a color as background for view
       } else { // Odd numbered row
          // set another color as background for view
       }
    
       ...
    
    }
    

    【讨论】:

      【解决方案2】:

      listView 可以用不同的颜色着色吗?

      是的,有可能。您可以使用带有自定义适配器的列表视图。

      关注这个tutorial ..

      【讨论】:

        【解决方案3】:

        是的,有可能,为此使用getView()

        public View getView(int position, View convertView, ViewGroup parent) {
        
           ViewHolder holder = (ViewHolder) convertView.getTag();
        
            if (position == 0){
            holder.layout.setBackgroundColor(Color.RED);
           }
            if (position == 1){
            holder.layout.setBackgroundColor(Color.BLUE);
           }
        }
        

        等等……

        【讨论】:

        • 你能解释一下持有人吗?这是什么
        猜你喜欢
        • 1970-01-01
        • 2015-05-03
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        • 2017-11-04
        • 2010-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多