2010-02-23 41 views
30

Tôi đang sử dụng mã sau để hiển thị nút ở cuối hoạt động.Làm cách nào để tạo chân trang cố định trong bố cục 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> 

và xem trên nó. khi tôi hiển thị thêm dữ liệu trong listview nút này pannel được di chuyển xuống. có thể bất kỳ một hướng dẫn tôi làm thế nào tôi có thể sửa chữa nó ở dưới cùng của hoạt động?

bất kỳ trợ giúp nào sẽ được đánh giá cao.

Trả lời

21

Thuộc tính android:layout_alignParentBottom phải được khai báo trong một phần tử của RelativeLayout không nằm trong chính số RelativeLayout (trừ khi có một số khác RelativeLayout làm cha mẹ).

Bạn nên làm một cái gì đó như thế này, với ListView bên trong RelativeLayout thêm:

<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

tôi đã cố gắng hàng giờ trong một ngày, nhưng điều này đã hoạt động ngay lập tức :) – ani0904071

95

Câu trả lời được lựa chọn như đúng là bị lỗi, nút sẽ che giấu phần dưới của giao diện danh sách. Cách chính xác là khai báo nút đầu tiên và định vị danh sách phía trên nút.

<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

Nếu bạn đã có, ví dụ, tất cả các yếu tố cuộn trong một scrollview, bạn nên làm như sau:

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

Lưu ý rằng nếu bạn muốn chân được cố định, sau đó bạn không nên đặt nó trong ScrollView, nơi mà nội dung cuộn được sẽ được đặt. Đặt con là RelativeLayout và đặt layout_alignParentBottom thành true. Có lẽ bạn sẽ cần phải thêm một padding ở dưới cùng của ScrollView trong trường hợp này (để phần tử cuối cùng không bị ẩn bởi chân trang).

Ý tưởng tương tự cho các yếu tố khác ngoài ScrollView

Các vấn đề liên quan