2012-06-06 24 views
9

如何讓textview精確地包裝這些文字?
如何讓textView精確地包裝其多行內容?

android:width屬性不是解決方案,因爲文本是動態的。

期望的行爲

|Adcs | 
|adscfd| 

當前水煤漿:

|Adcs  | 
|adscfd | 

Hereis代碼(風格TextViews的唯一定義的東西像文字顏色,TEXTSIZE,文本樣式)。

<TextView 
     android:id="@+id/text_title_holder" 
     style="@style/TextBold.Black.Title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:maxWidth="100dp" 
     android:maxLines="2" 
     android:text="Adcs adscfd" 
     android:gravity="left" 
     android:visibility="visible" /> 

專題wrap_content width on mutiline TextView沒有很好的答案。

+0

爲什麼不能在XML中使用'android:width = WRAP_CONTENT'? –

+0

刪除maxWidth屬性並嘗試 – Thamilvanan

+0

>>爲什麼不能只使用android:width = WRAP_CONTENT android:width屬性不接受「wrap_content」參數。它只接受數值。 –

回答

12

我遇到過這個問題,沒有在網上找到解決方案。我通過創建新組件TightTextView來做到這一點,即在指定了組件的最大寬度並且佈局(文本)的寬度小於視圖的測量寬度時重新測量給定文本。

package com.client.android.app.views; 

import android.content.Context; 
import android.text.Layout; 
import android.util.AttributeSet; 
import android.widget.TextView; 

/** 
* Tightly wraps the text when setting the maxWidth. 
* @author sky 
*/ 
public class TightTextView extends TextView { 
    private boolean hasMaxWidth; 

    public TightTextView(Context context) { 
     this(context, null, 0); 
    } 

    public TightTextView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public TightTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

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

     if (hasMaxWidth) { 
      int specModeW = MeasureSpec.getMode(widthMeasureSpec); 
      if (specModeW != MeasureSpec.EXACTLY) { 
       Layout layout = getLayout(); 
       int linesCount = layout.getLineCount(); 
       if (linesCount > 1) { 
        float textRealMaxWidth = 0; 
        for (int n = 0; n < linesCount; ++n) { 
         textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n)); 
        } 
        int w = Math.round(textRealMaxWidth); 
        if (w < getMeasuredWidth()) { 
         super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), 
           heightMeasureSpec); 
        } 
       } 
      } 
     } 
    } 


    @Override 
    public void setMaxWidth(int maxpixels) { 
     super.setMaxWidth(maxpixels); 
     hasMaxWidth = true; 
    } 

    @Override 
    public void setMaxEms(int maxems) { 
     super.setMaxEms(maxems); 
     hasMaxWidth = true; 
    } 
} 

!只是把它移植到舊的android API上,cuz getMaxWidth()僅在API級別16以後纔可用。

+0

+1,這個工作,應該是公認的答案 – h4lc0n

+1

我很困惑爲什麼在這裏maxWidth的東西是必要的。有了它,這段代碼並沒有解決我的問題。一旦我把它拉出來,它就起作用了。 –

+1

我有Math.round()的問題原因。最大行的最後一個字符被修剪。 用FloatMath.ceil()替換Math.round()解決了這個問題。 – HighFlyer

1

這個問題現在有點老了,但我也有這個問題,我想在一個mapView上的黑色框中使用綠色文本並通過將我的textView放在RelativeLayout容器中來解決它。然後我使用填充來設置邊框尺寸。 textView現在很好地擁抱了文字。我在eclipse中的輪廓看起來像這樣。

RelativeLayout 
    mapview 
    LinearLayout 
     RelativeLayout 
      textView1 << this is the box I want to hug the text 
     imageView1 
    RelativeLayout 
    etc.... 

希望這會有所幫助。

+0

你有沒有找到解決這個問題的辦法? – Uxonith