閱讀143 返回首頁    go 技術社區[雲棲]


為ListView增加Header

要實現類似IMBd類似的布局。

m1 m2

可以看出它的列表上方的圖片展示區和下麵列表條目是一體的,在上下滾動的時候。而圖片展示區不是列表條目。

當然可以分別實現,圖片展示區用Gallery,下麵用ListView,但是如果橫屏,則ListView部分的高度將很短,不方便上下滾動選擇。也可以強製豎屏,不過這樣用戶體驗會變差。

其實ListView提供了Header,IMBd實現的圖片展示區就是自定義的ListView Header。

寫個簡單的例子說明這個事情。先寫個最簡單的。

m3

這裏的header就是一個TextView生成的ListView Header部分。在布局的Listview部分:

<ListView android: 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);

源代碼見:

https://easymorse.googlecode.com/svn/tags/video.demo-0.1.0/

Header部分是可以添加多個的,比如:

image

對Header再做進一步定製。見效果:

image

這裏的Header部分使用了自定義視圖。

首先要增加一個小房子的圖:

image

然後,創建一個針對Header的layout:

image

該文件內容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:androhttps://schemas.android.com/apk/res/android%22">https://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));

源代碼見:

https://easymorse.googlecode.com/svn/tags/video.demo-0.1.1/

最後更新:2017-04-02 22:16:26

  上一篇:go html中表格table的內容居中顯示
  下一篇:go Ubuntu 11.04 添加筆記本的觸摸板的管理工具