[英]Android ListView with fixed header and footer
如何創建具有固定頁眉和頁腳的ListView
?
我不希望頁眉/頁腳滾動ListView
的項目。
頁眉/頁腳是否可能浮動在ListView
,以使頁眉/頁腳不需要具有直的底部/頂部背景,並且ListView
項目滾動到頁眉/頁腳視圖的背景下方,但仍顯示第一個元素列表?
我通過使用@blackbelt建議和一個小的ImageView來解決它,源圖像是透明的,具有平鋪背景。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@+id/tv_footer"
android:layout_below="@+id/tv_header" />
<TextView
android:id="@+id/tv_footer"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/footer_bg"
android:gravity="center"
android:text="Footer" />
<TextView
android:id="@+id/tv_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/header_bg"
android:gravity="center"
android:orientation="vertical"
android:text="Header" />
<ImageView
android:id="@+id/iconView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/lv"
android:background="@drawable/header_bg2"
android:src="@drawable/transparant_bg_tile" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_footer"
android:layout_alignParentRight="true"
android:background="@drawable/footer_bg2"
android:src="@drawable/transparant_bg_tile" />
</RelativeLayout>
設備截圖
http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29 。 檢查這個addHeaderView(param)。
http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29 。 檢查這是否為addFooterView(param)。
通過使用頁眉和頁腳按鈕調用layout @ Android listview來使用方法的示例
您可以使用addHeaderView和addFooterView為列表添加頁眉和頁腳。
你可以像@blackbelt建議的那樣做。 我使用了相對布局而不是LinearLayout。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@+id/textView1"
android:layout_below="@+id/tv1" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="Footer" />
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:text="Header" />
</RelativeLayout>
圖形布局快照
使用LinearLayout
,在ListView
和上面的頁LinearLayout
添加標題。 給ListView layout_weight="1"
使頁眉和頁腳分開位於ListView頂部和底部的視圖。 然后為這些視圖設置不透明度。
使用列表視圖代碼創建自定義頁眉和頁腳,如下所示
header.xml文件
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="your custom height" // you may set default too
/>
</RelativeLayout>
footer.xml文件
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
添加列表視圖的位置
LayoutInflater inflaterHeader = getLayoutInflater();
ViewGroup header = (ViewGroup) inflaterFooter.inflate(
R.layout.header, list_view, false);
yourListView.addHeaderView(header);
LayoutInflater inflaterFooter = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflaterFooter.inflate(
R.layout.footer, list_view, false);
yourListView.addFooterView(footer);
我們也可以用這種方式設置頁眉和頁腳,我在listview上面設置頁眉布局,同樣地我們在listview下面的to footer
<LinearLayout
android:id="@+id/ly_header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/app_theme_color"
android:orientation="horizontal">
<include layout="@layout/header_icuc"/>
</LinearLayout>
<ListView
android:id="@+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ly_header"
android:background="#F3F4F6"
android:divider="@drawable/contact_list_divider"
android:dividerHeight="2dp"
android:scrollbars="none" />
使用LinearLayout和ListView創建自己的自定義視圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.