2017-01-05 10 views
5

मैं ऐसे लेआउट के साथ गलत काम करता है (मैं कुछ विशेषताओं को हटा दिया है, कारण वे वास्तव में कोई फर्क नहीं पड़ता, पूर्ण डेमो परियोजना here है):TextView breakStrategy = "संतुलित" और layout_width = "wrap_content"

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content"  (* this is important) 
     android:layout_height="wrap_content" 
     android:breakStrategy="balanced"   (* this is important) 
     android:text="@string/long_text" /> 
</LinearLayout> 

टेक्स्ट long_text काफी लंबा है, इसलिए इसे कुछ पंक्तियों के लिए अलग किया जाएगा।

दो महत्वपूर्ण लाइनें android:layout_width="wrap_content" और android:breakStrategy="balanced" हैं।

मुझे उम्मीद है कि TextView वास्तविक टेक्स्ट चौड़ाई के अनुसार चौड़ाई की गणना करेगा, लेकिन ऐसा नहीं है।

screenshot

किसी को भी इसे ठीक करने का पता है?

अद्यतन

गुण android:breakStrategy="balanced" केवल API 23 और उच्चतर के लिए काम करता है। मेरे उदाहरण में पाठ 3 लाइनें लेता है, और रणनीति प्रत्येक पंक्ति को लगभग चौड़ाई बनाती है।

मुझे उम्मीद है कि इस मामले में दृश्य की चौड़ाई ही सबसे लंबी रेखा के समान होगी।

मैं किसी भी कामकाज की तलाश में हूं। मुझे Hangouts चैट में समाधान की आवश्यकता है। मैं यह नहीं समझ सकता कि यह कैसे काम करता है।

अद्यतन 2

दुर्भाग्य से, स्वीकार किए जाते हैं जवाब RTL पाठ के साथ काम नहीं करता।

+1

क्या करें आपका मतलब है 'मुझे उम्मीद है कि टेक्स्टव्यू वास्तविक पाठ चौड़ाई के अनुसार चौड़ाई की गणना करेगा, लेकिन यह एल्स नहीं है o आपकी वास्तविकता [इस तरह] की तरह अलग दिखती है (https://imagebin.ca/v/38OTigaIafpF) – shadygoneinsane

+0

मैंने प्रश्न अपडेट किया है। –

+0

@Oleksii Kropachov आप लाइनों को तोड़ने की उम्मीद कैसे करते हैं, यह 1.5 लाइनों से अधिक लंबा कहता है .. तो आपका अपेक्षित आउटपुट क्या है? –

उत्तर

7

ऐसा इसलिए होता है क्योंकि जब टेक्स्टव्यू इसकी चौड़ाई की गणना करता है तो यह सभी पाठ को 1 लाइन (आपके मामले में) के रूप में मापता है और इस चरण पर यह एंड्रॉइड के बारे में कुछ भी नहीं जानता: breakStrategy।इसलिए यह सभी उपलब्ध स्थान का उपयोग पहली पंक्ति पर जितना संभव हो उतना पाठ प्रस्तुत करने का प्रयास कर रहा है।

प्राप्त करने के लिए और अधिक जानकारी के, int desired(Layout layout)here

की जाँच करें विधि इसे ठीक करने के लिए आप इस वर्ग का उपयोग कर सकते हैं:

public class MyTextView extends TextView { 
public MyTextView(Context context) { 
    super(context); 
} 

public MyTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    Layout layout = getLayout(); 
    if (layout != null) { 
     int width = (int) Math.ceil(getMaxLineWidth(layout)) 
       + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
     int height = getMeasuredHeight(); 
     setMeasuredDimension(width, height); 
    } 
} 

private float getMaxLineWidth(Layout layout) { 
    float max_width = 0.0f; 
    int lines = layout.getLineCount(); 
    for (int i = 0; i < lines; i++) { 
     if (layout.getLineWidth(i) > max_width) { 
      max_width = layout.getLineWidth(i); 
     } 
    } 
    return max_width; 
} 

}

जिसे मैंने यहां ले गया - Why is wrap content in multiple line TextView filling parent?

+0

यह पूरी तरह से काम करता है। धन्यवाद! –

+0

आज मुझे एहसास हुआ है कि यह आरटीएल पाठ के साथ काम नहीं करता है। –

0

आप TextView में प्रोग्राम के पाठ की चौड़ाई को मापने के लिए है, इसलिए की तरह की आवश्यकता होगी: आप TextView के पीछे एक रंगीन आयत जगह कर सकते हैं, और पाठ को मापने के बाद इसकी चौड़ाई प्रोग्राम के सेट, अब

Rect bounds = new Rect(); 
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds); 

(मैं दूसरे के ऊपर दृश्य एक डाल करने के लिए FrameLayout उपयोग कर रहा हूँ, लेकिन आप के रूप में अच्छी RelativeLayout उपयोग कर सकते हैं):

एक्सएमएल:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View 
     android:id="[email protected]/background" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@color/green" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:breakStrategy="balanced" 
     android:text="@string/long_text" /> 
</FrameLayout> 

कोड:

Rect bounds = new Rect(); 
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds); 
View bg = findViewById(R.id.background); 
gb.getLayoutParams().width = bounds.width(); 

कोड अपरीक्षित है, लेकिन मुझे यकीन है कि आप बात समझ रहा हूँ।

अद्यतन

यह bounds.width() मिलान करने के लिए, एक दूसरे पृष्ठभूमि view का उपयोग कर TextView चौड़ाई निर्धारित करके बिना ऐसा करना संभव हो सकता है, लेकिन यह ट्रिगर कैसे पाठ टूटता में एक परिवर्तन है, इसलिए सावधान रहना चाहिए एक अनंत पाश का कारण नहीं है।

+0

मैंने पहले यह कोशिश की, लेकिन 'textView.getPaint()। GetTextBounds()' अपेक्षित के रूप में काम नहीं करता है। –

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