2011-06-08 66 views

回答

2
  1. 用于小区创建一个布局XML
  2. 对于行
  3. 创建一个布局XML在其中定义行(头)和低于 3.A. ListView中创建一个布局XML或者,您可以使用ListView的addHeaderView(View v)方法
  4. 创建自定义适配器并覆盖getView方法。检查每隔一行(位置%2 == 0)并更改行的颜色

我做了一个示例项目,可以从here下载它。

1)创建一个单元的布局XML

RES /布局/ cell.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:paddingLeft="4dp" 
    android:background="@drawable/item_light_bg" 
    /> 

如果你希望你的行是相同的宽度指定layout_width = “0dp” 和layout_weight = “1”。

2.),用于一个行创建一个布局XML

RES /布局/ list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<include 
    android:id="@+id/firstCol" 
    layout="@layout/cell" 
    /> 
<include 
    android:id="@+id/secondCol" 
    layout="@layout/cell" 
    /> 
<include 
    android:id="@+id/thirdCol" 
    layout="@layout/cell" 
    /> 
<include 
    android:id="@+id/fourthCol" 
    layout="@layout/cell" 
    /> 
<include 
    android:id="@+id/fifthCol" 
    layout="@layout/cell" 
    /> 
</LinearLayout> 

3.)创建一个布局XML在其中定义行(头)和这

RES /布局/ main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
<include 
     android:id="@+id/header" 
     layout="@layout/list_item" 
    /> 
<ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 
</LinearLayout> 

4)下面的ListView控件创建自定义适配器和奥雅纳使用getView方法。检查是否为每隔一行(位置%2 == 0),并更改行的颜色

public class MyAdapter extends SimpleAdapter { 
    ... 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LinearLayout v = (LinearLayout)super.getView(position, convertView, parent); 
     if (position % 2 == 0) 
      v.setBackgroundColor(Color.rgb(0, 0, 55)); 
     else 
      v.setBackgroundColor(Color.rgb(22, 22, 88)); 
     return v; 
    } 
    ... 

此外,您可能需要您的ListView是静态的(例如,不可点击和选择)。为此,请覆盖适配器中的isEnabled方法。

@Override 
public boolean isEnabled(int position) { 
    return false; 
} 
+0

这是我想要的东西,非常感谢你对此的解释和代码 – 2011-06-09 05:38:27

+0

示例项目链接是死的(http://dl.dropbox.com/u/296580/Test.zip) – 2015-04-15 05:30:10