【问题标题】:List view item background color列表视图项背景颜色
【发布时间】:2017-10-15 04:55:05
【问题描述】:

你好,我怎样才能从这个数组中设置项目衬垫布局背景颜色,它只有 3 种颜色,定位一种颜色,定位两种颜色,定位三种颜色,然后定位一种颜色,依此类推..

列表视图的位置

int[] androidColors = context.getResources().getIntArray(R.array.randomColor);
    viewHolder.linearLayout.setBackgroundColor(androidColors[position]); 

【问题讨论】:

  • 我认为模运算符 (position%3) 是您要找的。​​span>
  • @vishal-wadhwa 谢谢你,这是我想要的

标签: java android listview


【解决方案1】:

您可以尝试创建自己的自定义适配器并像这样实现 getView 功能:

public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
    //We must create a View:
    convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view.
//or an image on an ImageView.
return convertView;

}

试着看看这篇文章: Change background colour of current listview item in adapter getView method

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多