为ListView增加Header
ListView提供了Header,让我们可以轻松地建立起标题,以实现美化。
写个简单的例子说明这个事情。先写个最简单的。
这里的header就是一个TextView生成的ListView Header部分。在布局的Listview部分:
<ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" />
在代码中在ListView中加入Header:
listView = (ListView) this.findViewById(R.id.list);
TextView textView = new TextView(this);
textView.setText("header");
listView.addHeaderView(textView);
Header部分是可以添加多个的,比如:
对Header再做进一步定制。见效果:
这里的Header部分使用了自定义视图。
首先要增加一个小房子的图:
然后,创建一个针对Header的layout:
该文件内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:src="@drawable/ic_menu_home" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然后在代码中通过inflate的方式把layout加入到header。
listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.table_title, null));