【问题标题】:Android - custom listView, each row different coloursAndroid - 自定义listView,每行不同颜色
【发布时间】:2012-04-18 16:28:36
【问题描述】:

我创建了自定义列表视图,每一行看起来都像我的文件 custom_row.xml。有什么办法,如何分别为每一行设置不同的背景颜色(我需要设置它,因为我的行可以有不同的值)。

感谢您的建议

【问题讨论】:

    标签: android android-listview custom-lists


    【解决方案1】:

    由于您在 getView 方法中进行自定义列表视图 膨胀你的 custom_row.xml 后改变背景 inflate 方法的返回视图。请参阅下面的示例 sn-p:

    public getView(int position, View convertView, ViewGroup parent) {
           convertView = getLayoutInflater().inflate(R.layout.custom_xml, null);
           do some stuff...
    
           //let say you have an arraylist of color
           convertView.setBackgroundColor(arraylist.get(position));
    
           //in case that your color is limited, just re-use your color again
           //and some logic how to re-use the colors.
    }
    

    【讨论】:

      【解决方案2】:

      在adapter中,你可以在使用getView方法获取视图时,手动设置视图的背景色。

          // set the background to green
          v.setBackgroundColor(Color.GREEN);
      

      【讨论】:

        【解决方案3】:

        在此处使用其他答案会阻止选择器正常工作,并且选择器会完全停止突出显示行。通过按照接受的答案中的描述手动设置颜色,选择器在滚动时停止突出显示行。因此,让我描述一个不会弄乱您的选择器的解决方案。

        This article 正在描述如何以透明方式解决此问题,但我无法真正让它发挥作用。所以我的解决方案是我的可绘制文件夹中有两个列表选择器。这样我可以在运行时设置两种不同的背景颜色并保持选择器工作。

        list_selector_darkgrey.xml 用于我的深灰色线

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/gradient_bg_darkgrey" android:state_pressed="false" android:state_selected="false"/>
        <item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="true"/>
        <item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="false" android:state_selected="true"/>
        </selector>
        

        和 list_selector.xml 为浅灰色线

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/gradient_bg" android:state_pressed="false" android:state_selected="false"/>
        <item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="true"/>
        <item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="false" android:state_selected="true"/>
        </selector>
        

        请忽略drawable中的渐变,你可以用你的颜色替换它。

        在 BaseAdapter 类中,我将调用 setBackgroundResource 以将一些行显示为浅灰色,将其他行显示为深灰色。选择器颜色相同,并在 XML 文件中定义。

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
        
            if (convertView == null)
                vi = inflater.inflate(R.layout.list_row, null);
        
                     ... some logic ...
        
            if (... some condition ...) {
                vi.setBackgroundResource(R.drawable.list_selector_darkgrey);
            }
            else { 
                vi.setBackgroundResource(R.drawable.list_selector);
            }
            return vi;
        }
        

        【讨论】:

          猜你喜欢
          • 2017-05-14
          • 1970-01-01
          • 1970-01-01
          • 2011-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多