2012-12-25 10 views
5

मुझे ImageView के getWidth/getheight फ़ंक्शन के साथ समस्या हो रही है। मेरे एक्सएमएल इस प्रकार है के रूप में:एंड्रॉइड इमेज व्यू रिटर्न GetWidth, getHeight शून्य

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bonus_popup" 
    android:visibility="gone" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageView 
     android:id="@+id/todaybonus" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:adjustViewBounds="true" 
     android:src="@drawable/startscreen_todaybonus" /> 
    <ImageView android:id="@+id/ok_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="acceptBonus" 
     android:background="#000000"></ImageView> 
</RelativeLayout> 

और मेरे कोड इस प्रकार है:

if (isDisplayBonus){ 
    set_blur_background(); 
    bonusPopup.setVisibility(0); 
    Log.i(TAG, "Bonus width: " + todayBonus.getWidth()); 
    Log.i(TAG, "Bonus height: " + todayBonus.getHeight()); 
} 

कृपया मदद मुझे इस समस्याओं का समाधान ,. पहले ही, आपका बहुत धन्यवाद।

+0

यहां बोनस क्या है? कम से कम प्रासंगिक भाग पेस्ट करें। –

+0

हाय गौरव, बोनसपॉप –

+1

गतिविधि कोड है? ऊपर से नीचे तक। –

उत्तर

3

हो सकता है कि आप getWidth() और getHeight() बहुत जल्दी कॉल कर रहे हों। UI को अभी तक आकार में नहीं रखा गया है और स्क्रीन पर रखा गया है, और नतीजतन, विधियां सही ढंग से लौट रही हैं 0. अपने पूरे कोड की फिर से समीक्षा करें। या यहां अपना पूरा कोड पेस्ट करें।

+0

मैंने पहले ही अपना कोड यहां रखा है। दृश्य देखने के बाद मुझे GetWidth फ़ंक्शन कहा जाता है। –

+0

एक "हो सकता है" और "कृपया और दिखाएं" उत्तर – Dagon

8

ठीक है, मुझे कारण मिला है। जब मैंने दृश्यता निर्धारित की, तो दृश्य वास्तव में तैयार नहीं किया गया था, इसलिए मैं getHeight और getWidth फ़ंक्शन का उपयोग नहीं कर सका। मुझे समाधान मिला: ViewTreeObserver

todayBonus.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 
     int finalHeight = todayBonus.getMeasuredHeight(); 
     int finalWidth = todayBonus.getMeasuredWidth(); 
      // Do your work here 
      return true; 
    } 
    }); 
+1

आप इसे कहां रखते हैं? ऑनक्रेट विधि के अंदर? – yrazlik

+0

कृपया मुझे बताएं कि क्या आपको पता चला है – ChuckKelly

संबंधित मुद्दे