2011-08-31 35 views
0

我有一個TextView,它的高度是動態的。我想設置最後一行不可見,如果它不能完全繪製。 screenshotTextView最後一行畫成兩半

高度動態的原因是它在AppWidget中使用,並且appwidget在不同設備上的大小不一樣。因此,您無法測量TextView,也無法使用TextView的子類。 link

這就是我現在基本使用的。當我把一個長文本放到TextView中時,最後一行看起來很難看。

<LinearLayout 
android:id="@+id/widgetContent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
<TextView 
    android:id="@+id/sg" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 
+0

安置自己的TextView代碼once..so,我們可以做一些事情。 – Udaykiran

+0

如果你不在AppWidget中,它很好。有一個示例應用程序[Wiktinary](http://developer.android.com/resources/samples/Wiktionary/index.html)。它在那裏使用android:fadingEdge =「vertical」。它不是最好的,但我想即將使用它。 – jakk

回答

3

試試這個

Paint paint = new Paint(); 
// set paint with all current text properties of the textview. like text size, face etc, 

textPixelLenght = paint.measureText(mText); 
int textviewWidth = mTextView.getMeasuredWidth(); 
int numberOfLines = textPixelLenght /textviewWidth;; 

int textlineHeight = textview.getLineHeight(); 
if (textlineheight * numberOfLines > textviewHeight) { 
    // text going beyond textviews height. Ellipsize the text using 
    // TextUtils.ellipsize(params);  
} 
+0

這並不能解決問題,但是當你不在AppWidget中時,這是可以的。 – jakk

+0

這是不正確的使用textPixelLenght/textviewWidth LineNum – chefish

0

一個解決問題的方法是創建一個自定義的TextView和覆蓋的TextView的的onDraw()方法。在那裏你可以管理這個TextView處理這種情況的方式。

+1

這將是一個appwidget。所以擴展TextView並不好。 – jakk

1

你需要知道你的字體的間距 -

也就是說,如果你知道每行的20個字符,你知道每個全行10px的高,那麼你應該動態分配文本TextView的:

String toAdd = ""; 
for (int i=0; i < textViewHeight; i+= textHeight) 
    toAdd += fullText.subString(i, i+charactersPerLine); 
+0

我不知道每行有多少個字符。 – jakk

+0

如果你使用等寬字體,你可以很容易地解決它: http://en.wikipedia.org/wiki/Monospaced_font – Graeme

+0

是的,但等寬字體不看起來不錯。 – jakk

0

使用字體的高度動態改變視圖的高度,即

如果你的文字是10px的高,你的TextView的只能是10,20,30像素高。這並不要求你有任何特定的字體樣式。

+0

這聽起來不錯,但我不能在appwidget中做這些事情。我認爲這是一個XML屬性。 – jakk

+0

爲什麼你不能在AppWidget中做到這一點?沒有任何XML屬性可以滿足你的要求 - 如果你想要這樣做,你就必須爲此付出努力。 – Graeme

+0

更新AppWidget時,您不知道視圖的大小。 – jakk

0

該解決方案的兩倍措施通,但由於這是一個沒有孩子文本視圖的影響應該不會太大。這對小部件也沒有幫助,因爲它是自定義文本視圖。抱歉!

public class ExactWrappingTextView extends TextView{ 

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

private int measureHeight; 
private int extraSpace; 

//This doubles the measure pass for this view... but we have to know how 
    //high it would have been before we can pull off the right amount 
public void onMeasure(int width, int height){ 
    super.onMeasure(width, height); 
    extraSpace = (getMeasuredHeight() - (getPaddingTop() + getPaddingBottom())) % getLineHeight(); 
    measureHeight = MeasureSpec.makeMeasureSpec(getMeasuredHeight() - extraSpace, MeasureSpec.AT_MOST); 
    super.onMeasure(width, measureHeight); 
} 

}

4

我最近遇到這個問題,使用其他的解決方案。

我動態地設置MAXLINES:

final TextView tv = ((TextView) myLayout.findViewById(R.id.mytextview)); 
tv.setText(value); 
ViewTreeObserver vto = tv.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    private int maxLines = -1; 

    @Override 
    public void onGlobalLayout() { 
     if (maxLines < 0 && tv.getHeight() > 0 && tv.getLineHeight() > 0) { 
      int height = tv.getHeight(); 
      int lineHeight = tv.getLineHeight(); 
      maxLines = height/lineHeight; 
      tv.setMaxLines(maxLines); 
     } 
    } 
}); 
+0

工作像一個魅力感謝你的朋友 –

+0

也許這是正確的方法 – chefish