2012-01-25 23 views
6

मुझे लगता है कि मुझे लगता है कि बना रहा हूं अपने बच्चे TextViews का फ़ॉन्ट आकार पैमाने पर एक सेट चौड़ाई में सभी फिट करने के लिए होगा एक कस्टम दृश्य है, इसलिए प्रत्येक की अपनी लाइन पर है। जाहिर है कि इसे समझने के लिए चौड़ाई की आवश्यकता होगी। मैं तो जैसे onMeasure() अधिरोहित था:हैंडलिंग वजन

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
    int lineWidth = widthSize - getPaddingLeft() - getPaddingRight(); 

    // Change the text size until the largest line of text fits. 
    while (lineWidth < calculateTextWidth()) { 
    for (TextView textView : this.childViews) { 
     float newSize = textView.getTextSize() - 1; 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize); 
    } 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

calculateTextWidth() पाठ में से सबसे बड़ी लाइन की चौड़ाई की गणना करता है, और सब कुछ है कि के साथ काम करता है। इस कोड को FILL_PARENT और WRAP_CONTENT चौड़ाई के लिए ठीक ऊपर काम कर रहा है, लेकिन जब मैं शिकंजा घटक एक वजन देने के लिए और यह अपने वजन कि जिस तरह से स्वत: सेट कर सकें कोशिश - MeasureSpec.getSize(widthMeasureSpec) रिटर्न 0, getMinimumSuggestedWidth() करता है। यह एक अद्भुत गतिविधि त्रुटि का जवाब नहीं देता है, क्योंकि लाइनविड्थ हमेशा calculateTextWidth() से कम होगा, इसलिए while लूप हमेशा के लिए चलता है। XML कोड मैं का इस्तेमाल किया यह था, अनिवार्य रूप से:

<com.my.widgets.ScalingTextViewGroup 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" 
    > 
    <TextView 
     android:id="@+id/text_1" 
     android:text="Text 1" 
     android:textSize="20sp" /> 
    <TextView 
     android:id="@+id/text_2" 
     android:text="Text 2" 
     android:textSize="18sp" /> 
    <TextView 
     android:id="@+id/text_3" 
     android:text="Text 3" 
     android:textSize="18sp" /> 
    <TextView 
     android:id="@+id/text_4" 
     android:text="Text 4" 
     android:textSize="18sp" /> 
</com.my.widgets.ScalingTextViewGroup> 

मुझे समझ में क्यों यह 0 लौटा रहा है - जाहिर है, इसे 0 पर सेट किया जा रहा है - लेकिन मैं इसे layout_weight उपयोग करने के लिए कैसे मिलता है? मुझे लगता है कि यह ऐसा कुछ है जिसका सरल जवाब होना चाहिए, लेकिन मुझे यह नुकसान हुआ है कि यह क्या है।

उत्तर

10

मैंने कुछ महत्वपूर्ण गूगलिंग के माध्यम से इसे समझ लिया। this page पर मुझे पता चला कि ऑनमेजर को वास्तव में कहा जाता है जब android:layout_weight सेट होता है। मुझे बाद में पता चला कि इसका उल्लेख How Android Draws Views में किया गया है कि measure() को कई बार बुलाया जा सकता है, यह तुरंत मेरे दिमाग में नहीं बैठता था। पहला पास स्पष्ट रूप से प्रत्येक बच्चे के आकार के लिए मूल्य नहीं दे सकता है, क्योंकि यह नहीं जानता कि सभी व्यू भाई बहनों के वजन क्या हैं। यह इसके माध्यम से एक बार चला जाता है और अगर बच्चे किसी भी विशिष्ट बाधाओं को देखने के लिए एक MeasureSpec.UNSPECIFIED साथ 0 से चौड़ाई देता है, तो एक MeasureSpec.EXACTLY साथ वास्तविक वजन आवंटित करने के लिए फिर के माध्यम से चला जाता है। मेरी गतिविधि पहले पास पर खराब हो रही थी, इसलिए इसे कभी भी लेआउट चरण तक नहीं मिला। निम्नलिखित कोड पर मेरे ऑनमेजर() में संशोधन करने से समस्या ठीक हो गई है।

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
    if (widthMode != MeasureSpec.UNSPECIFIED) { 
    int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
    int lineWidth = widthSize - getPaddingLeft() - getPaddingRight(); 

    // Change the text size until the largest line of text fits. 
    while (lineWidth < calculateTextWidth()) { 
     for (TextView textView : this.childViews) { 
     float newSize = textView.getTextSize() - 1; 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize); 
     } 
    } 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
+0

तो यदि आप थोड़ी देर पहले बंद हो गए थे तो आपके पास निर्दिष्ट वजन के अनुसार अधिकतम उपलब्ध आकार होगा? (अब अपने लिंक पढ़ने के लिए जा रहे हैं ...) –

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

  • कोई संबंधित समस्या नहीं^_^