TextView最后一行画了一半

时间:2011-08-31 07:55:17

标签: android textview

我有一个TextView,其高度是动态的。我想设置如果无法完全绘制最后一行是不可见的。 screenshot

高度是动态的原因是它在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>

6 个答案:

答案 0 :(得分:5)

我最近遇到了这个问题并使用了其他解决方案。

我正在动态设置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);
        }
    }
});

答案 1 :(得分: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);     
} 

答案 2 :(得分:1)

您需要知道字体的间距 -

即,如果你知道每行20个字符,并且你知道每个完整行是10px高,那么你应该动态地将文本分配给TextView:

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

答案 3 :(得分:0)

您的问题的解决方案是创建自定义TextView并覆盖TextView的onDraw()方法。在那里你可以管理这个TextView处理这种情况的方式。

答案 4 :(得分:0)

使用字体的高度动态更改视图的高度,即

如果您的文字高10px,则TextView只能是10,20,30xx高。这不要求你有任何特定的字体样式。

答案 5 :(得分: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);
}

}

相关问题