2012-03-04 28 views
47

Tôi có LinearLayout dọc chứa ImageView và một vài bố cục và chế độ xem khác.ImageView trong bố cục XML android với layout_height = "wrap_content" có đệm trên & dưới

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

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/banner_alt" 
     android:src="@drawable/banner_portrait" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/main_search" 
     android:gravity="center" 
     android:textStyle="bold" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <Spinner 
     android:id="@+id/search_city_spinner" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:prompt="@string/search_city_prompt" 
     android:entries="@array/search_city_array" /> 

     <Spinner 
     android:id="@+id/search_area_spinner" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:prompt="@string/search_area_prompt" 
     android:entries="@array/search_area_array" /> 

    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <Spinner 
     android:id="@+id/search_rooms_spinner" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:prompt="@string/search_rooms_prompt" 
     android:entries="@array/search_rooms_array" /> 

     <Spinner 
     android:id="@+id/search_min_spinner" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:prompt="@string/search_min_prompt" 
     android:entries="@array/search_min_array" /> 

     <Spinner 
     android:id="@+id/search_max_spinner" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:prompt="@string/search_max_prompt" 
     android:entries="@array/search_max_array" /> 

    </LinearLayout> 

    <Button 
     android:id="@+id/saearch_button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/search_button" 
     android:onClick="searchButton" /> 

</LinearLayout> 

Vấn đề của tôi là khi hoạt động được hiển thị, ImageView có phần đệm ở đầu & dưới cùng. Tôi đã xác nhận nó là ImageView (bằng cách thiết lập một màu nền trên ImageView).

Hình ảnh có kích thước 450x450px. Đặt chiều cao theo cách thủ công thành 450px tạo hiệu ứng mong muốn (không có đệm) và đặt nó thành 450dp sẽ tạo ra hiệu ứng tương tự như khi sử dụng wrap_content.

Có vẻ như android đang lấy chiều cao của hình ảnh (450px) và đặt chiều cao của ImageView thành cùng một giá trị, nhưng trong dp.

Mọi ý tưởng về những gì tôi có thể làm để khắc phục sự cố này? Tôi không muốn sử dụng các giá trị tuyệt đối vì tôi sẽ cung cấp các hình ảnh khác nhau cho mật độ màn hình khác nhau.

Trả lời

127

Tôi gặp sự cố mô phỏng và đã giải quyết vấn đề bằng cách sử dụng android:adjustViewBounds="true" trên ImageView.

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:contentDescription="@string/banner_alt" 
    android:src="@drawable/banner_portrait" /> 
+13

Điều này đã làm cho tôi. Có vẻ không quan trọng rằng đây không phải là cài đặt mặc định. Đó là "wrap_content" - nhưng không thực sự ... – erlando

+2

Đã không làm việc cho tôi. Tôi đã kết thúc việc thêm trình nghe để thay đổi kích thước hình ảnh theo cách thủ công để khớp với tỷ lệ khung hình chính xác bất kỳ lúc nào bố cục của hình ảnh thay đổi. – gsteinert

+1

Điều đó làm cho ngày của tôi :-) –

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