2012-03-04 15 views
47

मेरे पास एक वर्टिकल लीनियरआउट है जिसमें एक छवि दृश्य और कुछ अन्य लेआउट और दृश्य हैं।लेआउट_हेइट = "wrap_content" के साथ एंड्रॉइड एक्सएमएल लेआउट में छवि दृश्य देखें शीर्ष और नीचे

<?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 पर मैन्युअल रूप से सेट करना वांछित प्रभाव (कोई पैडिंग) उत्पन्न करता है, और 450 डीपी पर सेट करने से wrap_content का उपयोग करने के समान प्रभाव उत्पन्न होता है।

ऐसा लगता है कि एंड्रॉइड छवि (450 पीएक्स) की ऊंचाई ले रहा है और छवि दृश्य की ऊंचाई को उसी मान पर सेट कर रहा है, लेकिन डीपी में।

कोई भी विचार यह है कि मैं इसे ठीक करने के लिए क्या कर सकता हूं? मैं पूर्ण मूल्यों का उपयोग नहीं करना चाहता क्योंकि मैं विभिन्न स्क्रीन घनत्व के लिए अलग-अलग छवियां प्रदान करूंगा।

उत्तर

127

मेरे पास एक समान समस्या थी और इसे छवि दृश्य पर 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

+2

मेरे लिए काम नहीं किया है। जब भी छवि परिवर्तन का लेआउट किसी भी समय सही पहलू अनुपात से मेल खाने के लिए छवि को मैन्युअल रूप से आकार देने के लिए श्रोता जोड़ना समाप्त हो गया है। – gsteinert

+1

जिसने मेरा दिन बनाया :-) –