具有固定页眉和页脚的Android ListView
如何创建具有固定页眉和页脚的具有固定页眉和页脚的Android ListView,android,android-listview,customization,Android,Android Listview,Customization,如何创建具有固定页眉和页脚的列表视图 我不希望页眉/页脚与列表视图中的项目一起滚动 是否有可能页眉/页脚浮动在列表视图上,因此页眉/页脚不需要直接的底部/顶部背景,并且列表视图项在页眉/页脚视图的背景下滚动,但仍然显示列表的第一个元素?使用线性布局,在列表视图和上面的页脚上添加页眉。提供列表视图布局\u weight=“1”使页眉和页脚在列表视图的顶部和底部分开。然后设置这些视图的不透明度。使用LinearLayout和ListView创建您自己的自定义视图。选中此项以查看addHeaderVi
列表视图
我不希望页眉/页脚与列表视图中的项目一起滚动
是否有可能页眉/页脚浮动在列表视图上,因此页眉/页脚不需要直接的底部/顶部背景,并且列表视图
项在页眉/页脚视图的背景下滚动,但仍然显示列表的第一个元素?使用线性布局
,在列表视图和上面的页脚上添加页眉。提供列表视图布局\u weight=“1”
使页眉和页脚在列表视图的顶部和底部分开。然后设置这些视图的不透明度。使用LinearLayout和ListView创建您自己的自定义视图。选中此项以查看addHeaderView(参数)
。选中此项以查看addFooterView(参数)
通过输入布局使用方法的示例@
可以对列表使用addHeaderView和addFooterView来添加页眉和页脚
你可以按照@blackbelt的建议去做。我使用了相对布局而不是线性布局
<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>
图形布局快照
我通过使用@blackbelt suggestion和一个小的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>
来自设备的屏幕截图
使用列表视图代码创建自定义页眉和页脚,如下所示
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);
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>
我们可以用这种方式设置页眉和页脚,我在listview上面设置页眉布局,用同样的方式我们可以在listview下面设置页脚
<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" />
这很好用。我使用了这个解决方案,但在页眉/页脚的下方和上方添加了一个透明的图像视图,背景为锯齿状效果。@Goran如果有帮助,请单击勾号标记答案。它将帮助其他访问post的人,这些post工作正常,适合我的线性布局,没有太多变化。谢谢
<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" />