简体   繁体   中英

getMeasuredHeight() of TextView with wrapped text

I'm trying to determine the height of a TextView before it is drawn. I'm using the following code to do that:

TextView textView = (TextView) findViewById(R.id.textview);
textView.setText("TEST");
int widthSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthSpec, heightSpec);
System.out.println("MeasuredHeight: " + textView.getMeasuredHeight());

The output is MeasuredHeight: 28 . Nothing wrong with that.

However, when I give the TextView a long text string, so wrapping occurs, it still gives the height of a single line instead of two:

(...)
textView.setText("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
(...)

The output is MeasuredHeight: 28 , where I would expect MeasuredHeight: 56 .

Why doesn't it give me the right value and how can I achieve the right value?

This is a onCreate method. Your whole view hierarchy isn't measured and layouted yet. So textView's parent doesn't know it's width. That is why textView's dimentions isn't constrained by it's parent's dimentions.

Try to change your line to:

int widthSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);

so it make your textview width equals 200 pixels.

If you explain why do you need textview's height maybe we will be able to help you.

Yeah, I recently bumped into this one myself. The best way is to create a ViewTreeObserver object and register a ViewTreeObserver.OnGlobalLayoutListener. This will be called after the layout phase and before the draw phase. You'll be able to get the size of your TextView at that point.

That won't strictly-speaking be the text itself, but for the entire TextView. If there's internal padding involved, the actual text will be smaller than the TextView.

If you really need the dimensions of the actual text, use textView.getLayout() to get the Layout object for the text, and then use layout.getHeight().

You have to create Custom Textview and use it in your layouts and use getActual height function to set the height at runtime

 public class TextViewHeightPlus extends TextView {
    private static final String TAG = "TextViewHeightPlus";
    private int actualHeight=0;


    public int getActualHeight() {
        return actualHeight;
    }

    public TextViewHeightPlus(Context context) {
        super(context);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

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

    }

   @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        actualHeight=0;

        actualHeight=(int) ((getLineCount()-1)*getTextSize());

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM