2010-11-21 20 views
13

Tôi gặp sự cố khi hiển thị ScrollView. Về cơ bản, bố cục là đầu trang và chân trang cố định với một scrollview ở giữa. Ý tưởng là khi bàn phím màn hình được kích hoạt cho nội dung EditText, hình ảnh có thể được cuộn nếu chiều cao của hình ảnh dài hơn điểm giữa.Android ScrollView thêm phần đệm bổ sung vào hình ảnh thu nhỏ trên cùng và dưới cùng của hình ảnh con

Trong thử nghiệm bố cục, tôi thấy rằng trên điện thoại Android của tôi (Xperia X10 Android 1.6) có một đống đệm được thêm vào phần trên cùng và dưới cùng của ImageView.

Tôi đánh giá cao bất kỳ đề xuất nào về cách tôi có thể ngăn điều này xảy ra.

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

    <!-- Header --> 
    <RelativeLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF"> 
     <TextView 
      android:text="Share your photo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left"/> 
     <Button 
      android:id="@+id/btnStack" 
      android:text="Stacks" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true"/> 
    </RelativeLayout> 
    <!-- End Header --> 


    <!-- Body --> 
    <ScrollView 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="5px" 
     android:background="#CCCCCC"> 
     <ImageView 
      android:id="@+id/imgPreview" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
    </ScrollView> 
    <!-- End Body --> 


    <!-- Footer --> 
    <RelativeLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" 
     android:layout_alignParentBottom="true"> 
     <EditText 
      android:id="@+id/caption" 
      android:text="Type your caption" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/caption" 
      android:background="#CCCCCC"> 
      <Button 
       android:id="@+id/btnUpload" 
       android:text="Share" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
      <Button 
       android:id="@+id/btnCancel" 
       android:text="Cancel" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
     </LinearLayout> 

    </RelativeLayout> 
    <!-- End Footer --> 

</LinearLayout> 

Trả lời

31

tôi tìm thấy sau khi một số thí nghiệm rằng việc thêm cú pháp sau để mã java của tôi giải quyết vấn đề:

imgPreview.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    imgPreview.setAdjustViewBounds(true); 
+1

Bất kỳ giải thích nào về lý do tại sao? –

+0

Tôi gặp sự cố này trên RelativeLayout bên trong ScrollView. Đưa adjustmentViewBounds vào xml của tôi không giải quyết được. Có nguyên nhân cho việc này không? Tôi nhận thấy rằng phần đệm được thêm ở trên cùng là chiều cao của thanh trạng thái và phần đệm ở phía dưới có cùng chiều cao với thanh điều hướng –

+0

Đã giúp tôi. Cảm ơn. – Gereltod

9
android:fillViewport="true" 
android:fadingEdge="none" 

Sử dụng các thuộc tính trong scrollview.

+0

Xin chào, đã thử nó nhưng dường như không tạo ra bất kỳ sự khác biệt nào. Chúng ta không nói về một vài điểm ảnh. Đó là một vài trăm. Ngay cả khi tôi chỉ định chiều rộng và chiều cao pixel của ImageView nó vẫn không hoạt động chính xác. –

+0

đã làm việc cho tôi tanx – Siddhesh

+0

Câu trả lời hay nhất! 'fillViewport' thực hiện công việc và cho phép bố cục con chiếm không gian được yêu cầu. – sud007

21

Chỉ cần thêm các thuộc tính sau đây để mô tả ImageView XML của bạn thực hiện công việc:

android:adjustViewBounds="true" 
0

bạn có thể đặt marginTop và dưới cùng thành số âm cho trẻ như thế này

<HorizontalScrollView 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:background="#ffffff" 
     android:overScrollMode="never" 
     android:scrollbars="none"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_marginBottom="-10dp" 
      android:layout_marginTop="-10dp"> 


    </LinearLayout> 

</HorizontalScrollView> 
Các vấn đề liên quan