2012-06-26 31 views
40

जब मैं singleLine="true" और ellipsize="end" (मेरी शीर्ष TextView), यह अच्छी तरह से काम करता हैTextView पर लपटने की स्थापना एक (के बजाय केवल पिछले लपेटने) द्वारा दिखाया गया लाइनों

साथ TextView उपयोग कर रहा हूँ लेकिन एक औरTextView में कम कर देता है 1 लाइनों के बाद (मेरे मामले में 0 नीचे TextView में 3 लाइनें), lines="3" and maxLines="3" and ellipsize="end",ठीक से काम नहीं करता है।

जब मैं tvDesc में ellipsize="end" नहीं डालता, तो यह 3 लाइन दिखाता है, जो ठीक है। एक्सएमएल:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgv" 
     android:layout_width="65dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/img1" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:id="@+id/tvTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/imgv" 
     android:background="@android:color/white" 
     android:textColor="@android:color/black" 
     android:text="Title testing line number and ellipsize at end" 
     android:maxLines="1" 
     android:singleLine="true" 
     android:ellipsize="end" <--- WORKS WELL 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/tvDesc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvTitle" 
     android:layout_toRightOf="@+id/imgv" 
     android:layout_alignBottom="@+id/imgv" 
     android:layout_alignParentRight="true" 
     android:textSize="14dp" 
     android:lines="3" 
     android:maxLines="3" 
         <---------- WITHOUT ellipsize 

     android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

कोड से ऊपर 3 लाइन्स के साथ TextView tvDesc और नो-लपटने से पता चलता यहाँ कोड है। यहाँ छवि है:

enter image description here

लेकिन, मुझे एल्लिप्साइज करना चाहते हैं, तो मैं followin कोड का उपयोग करता है: एक्सएमएल:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgv" 
     android:layout_width="65dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/img1" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:id="@+id/tvTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/imgv" 
     android:background="@android:color/white" 
     android:textColor="@android:color/black" 
     android:text="Title testing line number and ellipsize at end" 
     android:maxLines="1" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/tvDesc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvTitle" 
     android:layout_toRightOf="@+id/imgv" 
     android:layout_alignBottom="@+id/imgv" 
     android:layout_alignParentRight="true" 
     android:textSize="14dp" 
     android:lines="3" 
     android:maxLines="3" 
     android:ellipsize="end" <------ WITH ELLIPSIZE 

     android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

कौन सा केवल 2 लाइनों और लपटने साथ TextView tvDesc पता चलता है,

जो उचित रूप से डीजर्ड यूआई नहीं है, फॉलोइन: enter image description here

मैं साथ एल्लिप्साइज

किसी को भी मेरी मदद कर सकते TextView tvDesc में 3 लाइनों चाहता हूँ।?

+1

मुझे लगता है कि अगर आप किसी [ज्ञात समस्या] में चल रहा है (http: // stackoverflow। कॉम/प्रश्न/2160619/एंड्रॉइड-इलिप्सिज़-मल्टीलाइन-टेक्स्टव्यू) – tiguchi

+0

मुझे नहीं पता कि यह वास्तव में क्यों खुश है। लेकिन अगर आप इलिप्सिज़ के साथ जाना चाहते हैं तो आप अंत की बजाय अपनी कोई संपत्ति नहीं रखेंगे। –

उत्तर

44

यह अब तक का सबसे सरल समाधान है और मैं वर्तमान में तैनाती में उपयोग कर रहा हूं। अगर आपको किसी अन्य सहायता की ज़रूरत है तो मुझे बताएं!

ओह और अपने एक्सएमएल में android:ellipsize टैग को निकालना याद रखें क्योंकि आप 3 लाइनों के अंत में स्वचालित रूप से लंबित करने के लिए नीचे कोड का उपयोग करेंगे।

TextView snippet; 
snippet.setText("loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor ") 
ViewTreeObserver vto = this.snippet.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
     ViewTreeObserver obs = snippet.getViewTreeObserver(); 
     obs.removeGlobalOnLayoutListener(this); 
     if (snippet.getLineCount() > 3) { 
      int lineEndIndex = snippet.getLayout().getLineEnd(2); 
      String text = snippet.getText().subSequence(0, lineEndIndex - 3) + "..."; 
      snippet.setText(text); 
     } 
    } 
}); 
+0

धन्यवाद यार ... यह काम किया। बहुत बहुत धन्यवाद। –

+0

यह एक अच्छा समाधान है, लेकिन एक आसान और साफ तरीका है, मेरा जवाब देखें। – Flawyte

+1

धन्यवाद यह मेरे लिए काम करता है लेकिन अगर मैक्सलाइन और इलिप्सिज़ एक साथ काम करते हैं तो यह बहुत आसान होगा। – draksia

-2

यह

tv.setSingleLine(false); 
tv.setEllipsize(TextUtils.TruncateAt.END); 
int n = 3; // the exact number of lines you want to display 
tv.setLines(n); 

उल्लेख Programmatically create TextView with ellipsis

+0

क्या आपने इसे चेक किया है, यह मेरे लिए काम नहीं करता है। – vrs

+0

विश्व पटेल का जवाब देखें, यह पूरी तरह से काम कर रहा है। –

+0

संपादन देखें। मैं दो लाइनों को जोड़ने के लिए चूक गया। tv.setSingleLine (गलत); और tv.setLines (एन); –

-1

बस का उपयोग एल्लिप्साइजscrollHorizontally="true" के साथ संयुक्त प्रयास करें। सरल & साफ।

यह मेरे लिए पूरी तरह से काम किया।

+1

यह सिर्फ एक सिंगल लाइन बनाता है जिसे आप पहले से ही 'सिंगललाइन' प्रॉपर्टी –

+0

@AliGangji के साथ कर सकते हैं मुझे नहीं लगता कि यह पोस्ट किया गया था। – Flawyte

+0

जानना अच्छा है मेरे लिए यह समाधान multiline TextView के लिए भी काम करता है (एंड्रॉइड 5.0)। लेकिन टेक्स्ट व्यू के बीच लाइनों की संख्या बदलती है (1 से अधिकतम तक)। – CoolMind

16

बस android:maxLines और android:ellipsize सेट करें।

<TextView 
     android:id="@+id/tv_dua" 
     android:ellipsize="end" 
     android:maxLines="3" 
     android:text="long text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
+0

इसके अलावा यह हनीकॉम के संस्करणों पर काम नहीं करता है। मुझे जो मिलता है वह टेक्स्टव्यू 2 लाइनों के आकार में होता है, लेकिन आपको तीसरे पंक्ति पर अक्षरों के शीर्ष और कोई इलिप्सिस नहीं मिलता है। –

+0

किटकैट या लॉलीपॉप के आसपास कहीं भी यह सही ढंग से काम करना शुरू कर दिया (मैं सटीक एपीआई स्तर भूल गया)। इससे पहले यह अभी भी सही 'maxLines' तक सीमित है लेकिन इसमें इलिप्सिस भी शामिल नहीं है। फिर भी, मुझे लगता है कि इन दिनों रहने के लिए यह कुछ हद तक उचित है क्योंकि यह कोड में मैन्युअल स्ट्रिंग मैनिपुलेशन के लिए एक बहुत आसान विकल्प है। –

2

अंतिम पंक्ति पर अंडाकार के साथ एक बहु TextView प्राप्त करने के लिए निम्नलिखित का उपयोग करें:

android:maxLines="4" 
android:ellipsize="end" 
android:singleLine="false" 

लाइनों आप चाहते हैं की संख्या के साथ बदलें 4। उम्मीद है कि यह मदद करता है!

0

मैं इसे बाहर की कोशिश की Maxlines और एल्लिप्साइज साथ एंड्रॉयड 7 पर & 8.

android:maxLines="3" 
android:ellipsize="end" 

पूर्वावलोकन 2 लाइनों से पता चलता है और तीसरी पंक्ति "..." पर।

लेकिन यह पूर्वावलोकनकर्ता में एक बग प्रतीत होता है।

डिवाइस यह सिर्फ, ठीक काम करता है, पाठ के साथ 3 लाइनों तीसरी पंक्ति के अंत में "..."