2010-02-23 59 views
30

我正在使用以下代碼來顯示活動底部的按鈕。如何在android佈局中創建固定頁腳?

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     > 
    <Button android:id="@+id/btnGetMoreResults" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 

     android:text="Get more" 
     /> 
     </RelativeLayout> 

和它上面的列表視圖。當我在列表視圖中顯示更多數據時,此按鈕pannel會向下移動。任何人都可以引導我如何在活動底部修復它?

任何幫助,將不勝感激。

回答

21

android:layout_alignParentBottom屬性必須在RelativeLayout的元素中聲明,而不是在RelativeLayout本身中(除非另有RelativeLayout作爲父項)。

你應該做這樣的事情,與RelativeLayout也在裏面了ListView

<RelativeLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"   
android:layout_centerHorizontal="true"> 
    <ListView ...> 
    <Button android:id="@+id/btnGetMoreResults" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"  
    android:text="Get more" 
    android:layout_alignParentBottom="true" /> 
</RelativeLayout> 
+0

我嘗試了好幾個小時,每天背等元素相似,但這個工作瞬間:) – ani0904071 2016-05-26 04:54:59

95

選擇正確的答案是錯誤的,該按鈕將隱藏列表視圖的下部。正確的方法是首先聲明該按鈕並將該列表放置在按鈕上方。

<Button android:id="@+id/btnGetMoreResults" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"  
    android:text="Get more" 
    android:layout_alignParentBottom="true"/> 

<ListView 
    ... 
    android:layout_above="@id/btnGetMoreResults"/> 
5

如果你有,例如,所有在滾動型可滾動的元素,你應該做如下所示:

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
    android:layout_height="match_parent" 
     style="@style/rootElement" 
    > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 

      <!-- texts, buttons, images and anything that you want to scroll --> 

     </ScrollView> 

    <LinearLayout 
     android:orientation="vertical" 
     style="@style/footer" 
     android:id="@+id/footer"    
      android:layout_alignParentBottom="true" 
     > 

    </LinearLayout> 

</RelativeLayout> 

注意,如果你不想被固定的頁腳,然後你不應該把它放在可滾動內容將被放置的滾動視圖中。使其成爲RelativeLayout的子項並將layout_alignParentBottom設置爲true。在這種情況下,您可能需要在ScrollView的底部添加填充(以便最後一個元素不會被頁腳隱藏)。

的想法是比ScrollView