2012-05-29 24 views
8

Sự cố: ScrollView ẩn phần trên cùng của chế độ xem con khi nó phát triển vượt quá độ cao nhất định.ScrollView không hiển thị phần trên cùng

Tôi đã theo bố trí định nghĩa trong XML

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

    <LinearLayout 
     android:id="@+id/commandPanel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:paddingLeft="5dip" 
     android:paddingRight="5dip" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="OK" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Back" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/mainContentPanel" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@id/commandPanel" 
     android:background="@android:color/white" > 

     <ScrollView 
      android:id="@+id/formScrollView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:layout_marginTop="5dip" 
      android:layout_weight="1" 
      android:background="@android:color/darker_gray" 
      android:fillViewport="false" 
      android:paddingBottom="5dip" 
      android:scrollbarStyle="insideOverlay" > 

      <LinearLayout 
       android:id="@+id/formContentLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_marginTop="20dip" 
       android:background="@android:color/black" 
       android:gravity="center_horizontal|top" 
       android:orientation="vertical" 
       android:paddingLeft="5dip" 
       android:paddingRight="5dip" 
       android:paddingTop="20dip" > 

       <LinearLayout 
        android:id="@+id/tr" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dip" 
        android:orientation="horizontal" > 

        <TextView 
         android:id="@+id/title" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:text="sample title" /> 

        <TextView 
         android:id="@+id/value" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:text="sample value ......." /> 
       </LinearLayout> 
      </LinearLayout> 
     </ScrollView> 
    </RelativeLayout> 

</RelativeLayout> 

---------- đang ------

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.scrollview); 
    updateUi(); 
} 

//add few more rows here 
void updateUi() { 
    LinearLayout sampletr = (LinearLayout) findViewById(R.id.tr); 
    LinearLayout contentPane = (LinearLayout) findViewById(R.id.formContentLayout); 
    TextView title = (TextView) findViewById(R.id.title); 
    TextView value = (TextView) findViewById(R.id.value); 
    for (int i = 0; i < 25; i++) { 
     LinearLayout tr = new LinearLayout(this); 
     TextView t = new TextView(this); 
     t.setText("Sample title : " + i); 
     TextView v = new TextView(this); 
     v.setText("Sample value : " + i); 

     tr.addView(t, title.getLayoutParams()); 
     tr.addView(v, value.getLayoutParams()); 
     contentPane.addView(tr, sampletr.getLayoutParams()); 
    } 
} 

hàng đây hàng đầu trong LinearLayout (của scrollview ngay lập tức trẻ em) bắt đầu biến mất từ ​​đầu nếu không. hàng tăng hơn 15! Hơn nữa tôi thậm chí không thể di chuyển lên bằng tay để xem chúng, Bất kỳ con trỏ xin vui lòng, những gì sai với bố trí này?

+0

Có thể là bạn đặt paddingBottom trên ScrolView? thử và loại bỏ nó xem điều gì xảy ra. –

+0

@eric - nó không tạo ra bất kỳ sự khác biệt nào. – iuq

Trả lời

28

ok, tôi đã giải quyết nó bằng cách loại bỏ dòng sau từ LinearLayout (con trước mắt của scrollview)

android:layout_gravity="center" 

ScrollView hoạt động hoàn hảo ngay bây giờ. Cảm ơn mọi người đã đề xuất.

0

Tôi sẽ xóa nút Thứ hai RelativeLayout nhưng giữ lại ScrollView. Sau đó, cho số ScrollView tôi sẽ thêm layout_alignParentTop=truelayout_above="@+id/commandPanel". Thứ hai RelativeLayout có vẻ dư thừa và thường khi bạn thêm trẻ em vào một số RelativeLayout, chúng nên sử dụng các tùy chọn bố cục của vùng chứa, ví dụ: layout_above, bên dưới, căn chỉnh, v.v.

0

có vẻ như đầu đệm của bạn là một vấn đề , trước tiên hãy thử xóa nó.

+0

loại bỏ đệm không giải quyết được vấn đề hoặc – iuq

0

jus viết xml với sự thay đổi lil

<LinearLayout 
    android:id="@+id/commandPanel" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:paddingLeft="5dip" 
    android:paddingRight="5dip" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="OK" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Back" /> 
</LinearLayout> 

<RelativeLayout 
    android:id="@+id/mainContentPanel" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@id/commandPanel" 
    android:background="@android:color/white" > 

    <ScrollView 
     android:id="@+id/formScrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 

     android:layout_marginTop="0dp" <--- --- change n remove this margin 

     android:layout_weight="1" 
     android:background="@android:color/darker_gray" 
     android:fillViewport="false" 
     android:paddingBottom="5dip" 
     android:scrollbarStyle="insideOverlay" > 

     <LinearLayout 
      android:id="@+id/formContentLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:layout_marginTop="20dip" 
      android:background="@android:color/black" 
      android:gravity="center_horizontal|top" 
      android:orientation="vertical" 
      android:paddingLeft="5dip" 
      android:paddingRight="5dip" 
      android:paddingTop="20dip" > 

      <LinearLayout 
       android:id="@+id/tr" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dip" 
       android:orientation="horizontal" > 

       <TextView 
        android:id="@+id/title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="sample title" /> 

       <TextView 
        android:id="@+id/value" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="sample value ......." /> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

+0

cũng tôi đang thử nghiệm trên Moto Quench (OS 2.1) và tôi đã thử đề xuất của bạn nhưng vô ích. – iuq

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