如何测量基于设备宽度和字体大小TextView的高度? [英] How to Measure TextView Height Based on Device Width and Font Size?

查看:183
本文介绍了如何测量基于设备宽度和字体大小TextView的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找方法在Android中,将采取一个输入(文字,text_font_size,device_width),并根据这些计算将返回多少高度将需要显示特定的文字?

我设置根据他的内容文本视图/ web视图高度运行时,我注意到有关经的内容,但由于某些Web视图最小高度的问题,我不能在我的情况下使用。

所以,我想计算的高度,并基于该我设置视图的高度。

我曾尝试与以下几个方面

 油漆涂料=新的油漆();
paint.setTextSize(text.length());
矩形边界=新的矩形();
paint.getTextBounds(文字,0,1,边界);
mTextViewHeight = bounds.height();
 

所以输出

1)的Hello World返回13的高度为字体15

2)果冻豆的最新版本是在这里,与性能优化返回一个高度16的字体15

然后,我曾尝试与

 油漆涂料=新的油漆();
paint.setTextSize(15);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);

矩形边界=新的矩形();
paint.getTextBounds(文字,0,text.length(),结果);

Paint.FontMetrics度量= brush.getFontMetrics();
INT totalHeight =(INT)(metrics.descent  -  metrics.ascent + metrics.leading);
 

所以输出

1)的Hello World返回17的高度,字体15

2)果冻豆的最新版本是在这里,具有性能最佳化返回17的高度,字体15

如果我这些值设置为我的看法则其切割一些文本,它不显示的所有内容。

此外它看起来不错的一些表作为它具有大的宽度,但无法在手机上。

有什么方法来计算基于内容的高度?

解决方案

 公共静态INT的getHeight(上下文的背景下,字符串文本,诠释TEXTSIZE,诠释deviceWidth){
    TextView中的TextView =新的TextView(上下文);
    textView.setText(文本);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,TEXTSIZE);
    INT widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth,MeasureSpec.AT_MOST);
    INT heightMeasureSpec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec,heightMeasureSpec);
    返回textView.getMeasuredHeight();
}
 

如果 TEXTSIZE 不是以像素为单位给出,更改 setTextSize的第一个放慢参数()

I am looking for method in Android that will take a input (text, text_font_size, device_width) and based on these calculations it will return how much height will required to display particular text ?

I am setting a text view/ webview height runtime based on his content,I am aware about warp content but I can't use in my case because of some web view minimum height issue.

So I am trying to calculate height and based on that I am setting view height.

I have tried with following ways

Paint paint = new Paint();
paint.setTextSize(text.length()); 
Rect bounds = new Rect();
paint.getTextBounds(text, 0, 1, bounds);
mTextViewHeight= bounds.height();

So output is

1) "Hello World" returns a height of 13 for font 15

2) "The latest version of Jelly Bean is here, with performance optimizations" returns a height 16 for font 15

Then I have tried with

Paint paint = new Paint();
paint.setTextSize(15);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);

Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), result);

Paint.FontMetrics metrics = brush.getFontMetrics();
int totalHeight = (int) (metrics.descent - metrics.ascent + metrics.leading);

So output is

1) "Hello World" returns a height of 17 for font 15

2) "The latest version of Jelly Bean is here, with performance optimisations" returns a height of 17 for font 15

if I set these values to my view then its cutting some text, its not showing all content.

Again it looks OK on some tables as its having big width but not on phone.

Is there any way to calculate height based on content ?

解决方案

public static int getHeight(Context context, String text, int textSize, int deviceWidth) {
    TextView textView = new TextView(context);
    textView.setText(text);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST);
    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return textView.getMeasuredHeight();
}

If textSize is not given in pixels, change the first paramter of setTextSize().

这篇关于如何测量基于设备宽度和字体大小TextView的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆