android:列表视图布局以显示报告

时间:2011-06-08 06:32:35

标签: android android-listview

我需要创建列表视图来显示报告,其中包含带边框的列表标题,下方将包含列表视图,图片链接会更好地解释,请注意水平边框! here

谢谢。

1 个答案:

答案 0 :(得分:2)

  1. 为单元格创建布局xml
  2. 为行创建布局xml
  3. 创建一个布局xml,您可以在其中定义一行(对于标题)和下面的ListView 3.A.或者,您可以使用ListView的addHeaderView(View v)方法
  4. 创建自定义适配器并覆盖getView方法。检查是否每隔一行(位置%2 == 0)并更改行的颜色
  5. 我制作了一个示例项目,您可以从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,您可以在其中定义一行(对于标题)和下面的ListView

    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.。)创建自定义适配器并覆盖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;
    }