2010-06-27 55 views
29

Sự cố tôi gặp phải là nếu tôi có chế độ xem (ví dụ: myButton) bên trong RelativeLayout được đặt thành alignParentBottom - (theo mã bên dưới) - và sau đó quấn RelativeLayout bằng scrollview (cần thiết để nội dung có thể xem được trên màn hình nhỏ hơn hoặc khi định hướng thay đổi thành ngang) - khi đó, khi không thể nhìn thấy phía dưới gốc (ví dụ: khi được thay đổi thành hướng ngang) myButton được hiển thị ở đầu trang - KHÔNG ở dưới cùng.Android RelativeLayout: cách alignParentBottom khi được bao bọc trong ScrollView?

Làm cách nào để căn chỉnh chế độ xem ở cuối màn hình và giữ nó ở dưới cùng ngay cả khi phần dưới bên dưới cuộn?

<ScrollView 
android:id="@+id/mainScrollView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillViewport="true"> 



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/topLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 



... lots of views and stuff 


<Button android:layout_alignParentBottom="true" 
android:id="@+id/myButton" 
android:text="Click Me" /> 

</RelativeLayout> 
</ScrollView> 
+0

bạn có biết làm thế nào để làm điều này hôm nay? –

Trả lời

6

Sử dụng fill_parent làm chiều cao con của ScrollView là vô nghĩa. Bạn đang yêu cầu RelativeLayout luôn cao như cha mẹ của nó. Trong trường hợp này, ScrollView trở nên vô dụng! Chiều cao của RelativeLayout nên được đặt thành wrap_content, trong trường hợp này, tùy thuộc vào những gì RelativeLayout chứa, alignParentBottom có ​​thể không hoạt động như bạn mong đợi. Bạn chỉ cần sử dụng LinearLayout, nó sẽ đơn giản hơn rất nhiều.

+0

Vâng, tôi không hiểu tại sao bạn nói scrollview trở nên vô dụng, chức năng của nó là cho phép cuộn, và nó làm điều đó ... vì vậy tôi phải sử dụng nó. Tôi phải sử dụng RelativeLayout để tôi có thể sử dụng android_alignParentBottom. Tôi không thấy làm thế nào bằng cách sử dụng LinearLayout sẽ giải quyết điều này bởi vì sau đó tôi không thể sắp xếp myButton để dưới cùng của màn hình. Có thể không sắp xếp chế độ xem ở cuối màn hình và hỗ trợ di chuyển cùng một lúc không? – JohnRock

+0

Sẽ vô dụng nếu đứa trẻ có chiều cao = fill_parent. fill_parent có nghĩa là "Tôi muốn cao bằng bố mẹ". Vì vậy, nếu nội dung của ScrollView là lớn như ScrollView chính nó, nó sẽ không cuộn. –

+0

Ok. Tôi đánh giá cao sự hướng dẫn của bạn. Tuy nhiên, bố trí thực sự cuộn, đó không phải là vấn đề tôi đang cố gắng giải quyết. – JohnRock

3

tốt bạn có thể thử này ra

<LinearLayout android:id="@+id/parentLayout" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <ScrollView android:id="@+id/mainScrollView" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1" xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fillViewport="true"> 



     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/topLayout" android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 



      ... lots of views and stuff 


      <Button android:layout_alignParentBottom="true" android:id="@+id/myButton" 
       android:text="Click Me" /> 

     </RelativeLayout> 
    </ScrollView> 
</LinearLayout> 
+0

nhờ công việc của mình cho tôi –

+0

Cảm ơn bạn thân mến! Tôi phải đối mặt với cùng một vấn đề và bắt đầu nghĩ rằng điều đó là không thể. Giải pháp của bạn giúp tôi rất nhiều – Androider

24

Hãy nút ra khỏi scrollview, Quấn cả scrollview và Button trong một Linear Layout, thiết lập chiều cao của scrollview để 0dip và đặt nó là trọng lượng tới 1, không đặt bất kỳ thuộc tính trọng số nào cho nút, vì vậy scrollview có thể chiếm toàn bộ không gian còn lại chỉ tiết kiệm khoảng cách không gian của nút ở phía dưới.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

     ....lots of views... 


     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:text="Button" /> 

</LinearLayout> 
+7

Nhưng điều này sẽ làm cho 'Nút' luôn luôn ở dưới cùng của màn hình ngay cả khi 'ScrollView' có thể cuộn được, phải không? –

+0

yeah nó sẽ: D – kunmi

+0

Đây là một giải pháp tôi thích, nhưng hãy cẩn thận rằng "trọng lượng" là xấu cho hiệu suất – superuser

-1

Đó là một chút muộn, nhưng là giải pháp đơn giản nhất là thêm

android:below="@id/lots_of_stuffs" 

Như thế này:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainScrollView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true"> 

    <RelativeLayout 
    android:id="@+id/topLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 


    <LinearLayout 
     android:id="@+id/lots_of_stuffs" > 

     ... lots of views and stuff 

    </LinearLayout> 

    <Button android:layout_alignParentBottom="true" 
     android:id="@+id/myButton" 
     android:text="Click Me" 
     android:below="@id/lots_of_stuffs" /> <!-- Here is the "hack" --> 

    </RelativeLayout> 
</ScrollView> 
+0

bit trễ và vẫn không phải là một giải pháp thích hợp, điều này sẽ kéo dài nút để chiều cao điền của nó. – Shivansh

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