[英]Android ListView Footer View not being placed on the bottom of the screen
我確信我錯過了一些簡單的方法來實現這一點,但是我已經完成了所有已知的組合,以便得到我正在尋找的工作。 當ListView項目沒有填滿頁面時,我正試圖讓ListView頁腳位於屏幕底部。
例如,我有一個包含三個項目的ListView的頁面和一個FooterView(使用ListView的addFooterView)我希望footerView位於屏幕的底部。 當ListView包含足夠的項目來滾動屏幕時,footerView應位於列表的末尾(而不是位於屏幕的底部)。
我正在嘗試使用的XML如下所示。 任何和所有的幫助非常感謝!
Layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@+drawable/background">
<ListView
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@android:color/transparent"
android:divider="@null">
</ListView></RelativeLayout>
ListViewRow.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
style="@style/Nationwide.menu">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_item"
style="@style/Nationwide.menu.row">
<ImageView
android:id="@+id/image_icon"
style="@style/Nationwide.menu.leftIcon" />
<TextView
android:id="@+id/nav_text"
style="@style/Nationwide.menu.text" />
<ImageView
android:id="@+id/chevron_symbol"
style="@style/Nationwide.menu.rightIcon" />
</LinearLayout>
<View
android:layout_height="1dp"
android:background="@drawable/nav_item_divider_dark" />
<View
android:layout_height="1dp"
android:background="@drawable/nav_item_divider_light" /></LinearLayout>
ListViewFooter.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<View
android:id="@+id/footer_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true" /></RelativeLayout>
Java的
LayoutInflater inflater = this.getLayoutInflater();
View footerView = inflater.inflate(R.layout.footer, null);
footerView.findViewById(R.id.footer_image).setBackgroundDrawable(resources.getDrawable(R.drawable.cone_footer));
mMenuListView.addFooterView(footerView);
到目前為止我嘗試過的事情包括:
提前致謝!
我希望footerView位於屏幕的底部。
這不是addFooterView()
工作原理。
當ListView包含足夠的項目來滾動屏幕時,footerView應位於列表的末尾(而不是位於屏幕的底部)。
這也不是addFooterView()
工作原理。
如果你想讓它與列表一起滾動(即,如果三個列表項占據屏幕的上半部分,頁腳就在三個項目的正下方),然后使用addFooterView()
...但頁腳總是滾動與列表。
如果你不希望它不滾動列表,一種方法是使用RelativeLayout
作為你的父,將你的頁腳錨定到屏幕的底部,並讓ListView
坐在頂部和頁腳之間...但是然后頁腳從不滾動。
如果其他人正在尋找類似的答案,這就是我最終要解決的問題。 由於我想要添加的“頁腳”只是一個圖像來填充屏幕中的一些空間(因為視圖很短),而ListView方法並不是我實現所需的,我們最終得到了這個作為我們的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
style="@style/company.mainContainer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- enter page content here -->
<View android:layout_height="0px" android:layout_weight="1"
android:visibility="invisible" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:scaleType="fitXY"
android:src="@drawable/footer_cone">
</ImageView>
</LinearLayout>
</ScrollView>
希望這有助於他人!
像這樣
<RelativeLayout>
<include android:id="@+id/header" layout="@layout/header" android:layout_width="fill_parent" android:layout_alignParentTop="true" />
<include android:id="@+id/footer" layout="@layout/footer" android:layout_width="fill_parent" android:layout_alignParentBottom="true" />
<ListView android:layout_below="@id/header" android:layout_above="@id/footer"/>
</RelativeLayout>
這是我用來實現這種行為的邊緣技巧。
基本上你將ListView
設置為wrap_content
,然后使用負邊距將Button
放在ListView
底部邊距空間中。
這使用固定高度,但你也可以在運行時測量按鈕,並設置正負邊距。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_alignParentTop="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/list_item"
android:paddingBottom="@dimen/margin"
android:layout_topMargin="-50dp"
android:paddingTop="@dimen/margin"
android:text="@string/add" />
</RelativeLayout>
你要做的事實上是相當復雜的。 您希望“頁腳”與列表一起滾動(有時至少),這意味着它需要包含在ListView布局中。 但是你也(其他時候)想要相對於屏幕放置相同的頁腳,或者更准確地說相對於ListView的某些父布局(或父級父級等),這意味着頁腳不需要包含在ListView中。 這兩種情況需要相互排斥地放置有問題的元素。
不過,這在SDK的設計上並不差。 從您的描述看來,您可能會以錯誤的方式思考問題。 從概念上講,這個元素是否真的是一個頁腳,如果它不總是在列表的“腳”,但有時一直在屏幕的底部? 你可以通過組合ListViews,ScrollViews,RelativeLayouts和一些android:layout_height技巧來制作你想要的那種布局。 然而,簡單地采用替代方法來設計可能是一個更好的舉措。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.