我需要创建列表视图来显示报告,其中包含带边框的列表标题,下方将包含列表视图,图片链接会更好地解释,请注意水平边框! here
谢谢。
答案 0 :(得分:2)
我制作了一个示例项目,您可以从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;
}