【问题标题】:Android Alternate row Colors in ListViewListView中的Android备用行颜色
【发布时间】:2012-10-18 01:43:12
【问题描述】:
public class ListView extends  ListActivity {

static String item;

public void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, Str.S);
            setListAdapter(adapter);

      }

这是我的列表视图类,它工作得很好,它从一个名为 Str 的类中获取字符串并将它们显示在列表视图中,问题是列表视图样式不好,它是黑色的,字符串是白色的。

我希望它们可以替代每一行都有一种颜色。

我尝试了很多教程,但没有一个足够清楚.. 如何为每一行制作替代颜色 .. ex。第 1 行蓝色,第 2 行白色,第 3 行蓝色,第 4 行白色,等等。

【问题讨论】:

  • 你能给我们举一些你尝试过的例子吗?那么就更容易解释哪里出了问题

标签: android listview


【解决方案1】:

Here 是如何做到这一点的。

这里简要给出我的示例代码:

覆盖适配器中的getView 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {  
View view = super.getView(position, convertView, parent);  
if (position % 2 == 1) {
    view.setBackgroundColor(Color.BLUE);  
} else {
    view.setBackgroundColor(Color.CYAN);  
}

return view;  
}

覆盖 ArrayAdapter 并覆盖那里的 getView 方法。

所以如果你的适配器是这样的:

public class MyAdapter extends ArrayAdapter

你的ListActivity 会变成这样:

 ArrayAdapter<String> adapter = new MyAdapter<String>(this,
                android.R.layout.simple_list_item_1, Str.S);

Here's an example 关于重写 ArrayAdapter。

【讨论】:

  • 我试过了,没有错误,但是如何将它实现到我的列表视图..?
  • 嗯,我的整个班级都在讨论它的 ListActivity 而不是 ArrayAdapter .. IDK 我没有 getView 该怎么办,所以我如何覆盖它..
  • 看,你不必重写任何ListActivity。创建一个扩展 ArrayAdapter 的新类 MyAdapter。现在在上面显示的类中,将 ArrayAdapter 更改为 MyAdapter,正如我在答案中提到的那样。而已!如果您还有其他问题,请告诉我。
  • 好的,谢谢。我得到两个错误。 1-从 MyAdapter 在我的类中更改“MyAdapter 类型不是通用的;它不能用参数 参数化”和从 MyAdapter 类“隐式超级构造函数 ArrayAdapter() 未定义为默认构造函数。必须定义显式构造函数”
  • 向我展示你的 ArrayAdapter 实现。即 MyAdapter 类。
【解决方案2】:

自定义列表视图行的背景颜色可以用

设置
row.setBackgroundResource(R.color.list_bg_2)

自定义列表视图适配器中的方法

getView(int position, View convertView, ViewGroup parent)

我尝试了很多类似row.setBackgroundColor(0xFF00DD) 的方法,但都无法完成,

这里的list_bg_2是一个颜色集res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="list_bg_1">#ffffff</color>
    <color name="list_bg_2">#fef2e8</color>
</resources>

【讨论】:

    【解决方案3】:
    if (position % 2 == 0) {
    
        rowView.setBackgroundColor(Color.parseColor("#A4A4A4"));
    
    } else {
    
        rowView.setBackgroundColor(Color.parseColor("#FFBF00"));
    
    }
    

    【讨论】:

      【解决方案4】:

      如果视图是 ViewGroup,简单的背景设置不起作用

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {  
          final int rr = (position % 2 == 0) ? R.color.border_end_1 : R.color.black;
          final int cc = getResources().getColor(rr);
          View view = super.getView(position, convertView, parent);  
          walk(view, rr, cc);
          return view;  
      }
      private void walk(View view, int rr, int cc){
          view.setBackgroundResource(rr);
          ViewGroup group = (ViewGroup)view;
          int nc = group.getChildCount();
          for (int i = 0; i < nc; i++) {
              final View v = group.getChildAt(i);
              if (v instanceof ViewGroup)
                  walk(v, rr, cc);
              else
                  v.setBackgroundColor(cc);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        相关资源
        最近更新 更多