2012-04-21 25 views
2

我有一個垂直的LinearLayout,它有一個列表視圖和一個ImageView在底部,一個在頂部,列表視圖填滿了留下的空間。如何使android:layout_gravity =「bottom | center_horizo​​ntal」停留在底部

它看起來像這樣:

<LinearLayout layout_height="match_parent" layout_width="@dimen/width" 
    orientation="vertical"> 
    <ImageView layout_height="@dimen/width" layout_width="@dimen/width" id="@+id/top"> 

    <ListView android:layout_height="0px" 
     android:background="#00ffff" 
     android:layout_width="@dimen/width" 
     android:layout_weight="1" /> 

    <ImageView layout_height="@dimen/width" layout_width="@dimen/width" id="@+id/bottom" layout_gravity="bottom|center_horizontal"> 

它正常工作,每當我有肉眼可見的列表視圖。

但是,無論何時將ListView設置爲可見性消失,「底部」ImageView都會彈出到頂部圖像視圖的正下方。

我的問題是爲什麼儘管我說'android:layout_gravity =「bottom | center_horizo​​ntal'底部的ImageView並沒有停留在底部''我查看了hierarchyViewer,父級的高度與屏幕的高度相匹配。所以底部的Image視圖應該遵循android:layout_gravity =「bottom | center_horizo​​ntal」並留在底部,對吧?

+0

你需要'ListView'是'gone',或者你可以用它設置爲'invisble'脫身?我很確定後者會給你你想要的。 – 2012-04-21 03:12:27

回答

3

作爲替代我的意見,您可以將LinearLayoutRelativeLayout取代。 Agarwal已經提出了這個建議,但是他的代碼有點亂,在寫這篇文章的時候,你想要做的事情不正確。

給下面的代碼一個嘗試。當您將ListView設置爲gone時,頂部和底部圖像將保持與visible相同。請注意,我更換了@dimens/width引用,所以你可能希望把那些回。

<?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"> 

    <ImageView android:id="@+id/top" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <ListView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:layout_above="@+id/bottom" 
     android:layout_below="@+id/top" android:background="#00ffff" /> 

    <ImageView android:id="@+id/bottom" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 
+0

謝謝。是否有可能使ListView從底部開始「增長」,而不是頂部。即第一個元素顯示在ListView的底部,然後第二個元素位於第一個元素上方,子元素等等? – michael 2012-04-23 19:08:31

+0

@michael:如果我沒有弄錯,你可以使用'stackFromBottom'標誌來達到這個目的。或者通過xml設置它:['android:stackFromBottom'](http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:stackFromBottom),或者代碼:[''setStackFromBottom(boolean)'] (http://developer.android.com/reference/android/widget/AbsListView.html#setStackFromBottom%28boolean%29)。 *「當底部堆棧設置爲true時,列表將從視圖底部開始填充其內容。」* – 2012-04-23 19:21:57

0

最好的解決方案是使用ralaticelayout這個::::

<?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="#0000FF"> 

<ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/top" /> 
<ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/bottom" android:layout_alignParentBottom="true"/> 
    <ListView android:layout_height="fill_parent" 
     android:background="#00ffff" 
     android:layout_width="fill_parent" 
     android:layout_below:@id/top android:layout_above:@id/bottom /> 


</RelativeLayout> 
+1

我很抱歉,但你的建議毫無意義。帶有'orientation'屬性的'RelativeLayout'?和一個重量的孩子看法?這實際上總是會呈現高度爲'0'的ListView,換句話說它根本不可見。最後,'ListView'和底部'ImageView'將疊加,這可能不是OP後面的內容... – 2012-04-21 03:15:40

+0

刪除了方向,它不需要relativelayout。 0dp表示佔用剩餘空間。 – 2012-04-21 03:23:13

+0

你真的嘗試過你的建議嗎?據我所知,「RelativeLayout」中的權重被忽略,意味着它的固定高度爲零... – 2012-04-21 03:26:58

相關問題