簡體   English   中英

如何在android布局中創建固定頁腳?

[英]How to create fixed footer in android layout?

我正在使用以下代碼在活動底部顯示按鈕。

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

並在其上方的列表視圖。 當我在列表視圖中顯示更多數據時,此按鈕面板將向下移動。有人可以指導我如何在活動底部修復它?

任何幫助,將不勝感激。

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

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

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> 

例如,如果您在ScrollView中具有所有可滾動的元素,則應該執行以下操作:

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

請注意,如果要固定頁腳,則不應將其放在將放置可滾動內容的ScrollView中。 使其成為RelativeLayout的子級,並將layout_alignParentBottom設置為true。 在這種情況下,也許您需要在ScrollView的底部添加一個填充(以使頁腳不會隱藏最后一個元素)。

對於ScrollView以外的其他元素,此想法類似

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM