2012-03-04 83 views
47

我有一個垂直LinearLayout包含一個ImageView和一些其他佈局和視圖。ImageView在layout_height =「wrap_content」的android XML佈局中有填充頂部和底部

<?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> 

我的問題是,在顯示活動時,該ImageView的在頂部&底部填充。我已經確認它是ImageView(通過在ImageView上設置背景顏色)。

圖片是450x450px。手動設置高度爲450px會產生所需的效果(無填充),並將其設置爲450dp會產生與使用wrap_content相同的效果。

看來,Android正在採取圖像的高度(450px),並將ImageView的高度設置爲相同的值,但在dp。

任何想法,我可以做些什麼來解決這個問題?我不想使用絕對值,因爲我將爲不同的屏幕密度提供不同的圖像。

回答

127

我有一個類似的問題,並在ImageView上使用android:adjustViewBounds="true"解決它。

<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

這對我來說伎倆。這似乎違反直覺,這不是默認設置。這是「wrap_content」 - 但不是真的... – erlando 2012-07-27 13:37:52

+2

還沒有爲我工作。我最終添加了一個監聽器,在圖像佈局發生變化時手動調整圖像大小以匹配正確的寬高比。 – gsteinert 2012-08-13 06:16:19

+1

這使我的一天:-) – 2012-11-15 11:52:17

相關問題