开发者

TextView last line drawn in half

开发者 https://www.devze.com 2023-04-01 03:31 出处:网络
I have a TextView which height is dynamic. I want to set that the last line wouldnt be visible if it cannot be fully drawn.

I have a TextView which height is dynamic. I want to set that the last line wouldnt be visible if it cannot be fully drawn. screenshot

The reason why height is dynamic is that it is used in an AppWidget, and the size of an appwidget is not the same on different devices. Beca开发者_如何学Gouse of this you cannot measure the TextView or cannot use a subclass of a TextView. link

This is what I use now basically. When I put a long text into the TextView the last line looks ugly.

<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>


I've faced this problem recently and used another solution.

I'm setting the maxLines dynamically:

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);
        }
    }
});


Try this

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);     
} 


You need to know the spacing of your font -

ie, if you know its 20 characters per line, and you know each full line is 10px high, then you should dynamically assign the text to the TextView:

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


A solution to your problem would be to create a custom TextView and override the onDraw() method of the TextView. There you could manage the way this TextView handles this case.


Using the height of the font dynamically change the height of the view, ie

If your text is 10px high, your TextView can only be 10, 20, 30px high. This doesn't require you to have any particular style of font.


This solution doubles the measure pass, but because this is a text view with no children the impact shouldn't be too great. This also won't help with a widget, as it's a custom text view. Sorry!

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);
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号