2012-08-26 37 views
6

Tôi đang tạo ứng dụng cho thiết bị Android và tôi đang cố gắng có ScrollView bên trong LinearLayout nhưng khi tôi cố gắng thực hiện việc này, ScrollView mất tất cả không gian và các phần tử sau ScrollView trong LinearLayout disapear.Android: ScrollView ở dọc LinearLayout

Ví dụ:
Nếu scrollview không phải là "đầy đủ":
http://kakko76.free.fr/scroll2.jpg
Nếu scrollview là "đầy đủ":
http://kakko76.free.fr/scroll1.png

Như bạn có thể thấy nút disapear ...
Đây là mã của Hoạt động này:

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_marginLeft="50dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:orientation="vertical" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:background="@drawable/linearlayoutbackground" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/nom_des_joueurs" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_marginBottom="30dp" /> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:id="@+id/llPlayersName" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      > 

     </LinearLayout> 

    </ScrollView> 

    <Button 
     android:id="@+id/okPlayersName" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/ok" 
     android:background="@drawable/backgroundbutton" 
     android:layout_marginTop="30dp" /> 
</LinearLayout> 

Sau khi tôi thêm các phần tử vào Linea rLayout đang ở trong ScrollView.
Bất kỳ giải pháp nào?
Cảm ơn.

Trả lời

9

Hãy thử như sau:

android:layout_height="0dp" 
android:layout_weight="1" 

này cho scrollview của bạn để thực hiện tất cả các không gian còn lại không bị chiếm đóng bởi các yếu tố khác.

+1

Đã viết cùng một giải pháp cùng một lúc :-p bạn đã nhanh hơn .. –

+0

Cảm ơn bạn đó là công việc :) – kakko76

1

bạn có android:layout_height="wrap_content" làm chiều cao của mình. điều đó có nghĩa là chiều cao là cao như nội dung bên trong nó. nếu bạn không muốn nó chiếm toàn bộ nơi bạn có thể cho nó trọng lượng (như câu trả lời ở trên) hoặc bạn có thể thiết lập hight của bạn với dp. ví dụ:

<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_marginLeft="50dp" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 
android:orientation="vertical" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:background="@drawable/linearlayoutbackground" 
android:weightSum="1.5" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight = "0.2" 
    android:text="@string/nom_des_joueurs" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_marginBottom="30dp" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight = "1"> 

    <LinearLayout 
     android:id="@+id/llPlayersName" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 

    </LinearLayout> 

</ScrollView> 

<Button 
    android:id="@+id/okPlayersName" 
    android:layout_width="match_parent" 
    android:layout_height="0" 
    android:layout_weight = "0.3"> 
    android:text="@string/ok" 
    android:background="@drawable/backgroundbutton" 
    android:layout_marginTop="30dp" /> 

như bạn có thể thấy tôi đã cho bố trí tuyến tính một "tổng" và trọng lượng tất cả các con của ông được đưa ra. có rõ ràng không? đó là những gì bạn cần?

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