我在Android中寻找将采用输入(text,text_font_size,device_width)的方法,并根据这些计算,它将返回显示特定文本所需的高度?
我正在根据他的内容设置文本视图/ webview高度运行时,我知道warp内容但我不能在我的情况下使用因为一些web视图最小高度问题.
所以我试图计算高度,并根据我设置视图高度.
我尝试过以下方法
Paint paint = new Paint();
paint.setTextSize(text.length());
Rect bounds = new Rect();
paint.getTextBounds(text, 0, 1, bounds);
mTextViewHeight= bounds.height();
Run Code Online (Sandbox Code Playgroud)
所以输出是
1)"Hello World"返回字体15的高度13
2)"Jelly Bean的最新版本在这里,性能优化"返回字体15的高度16
然后我试过了
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);
Run Code Online (Sandbox Code Playgroud)
所以输出是
1)"Hello World"返回字体15的高度17
2)"最新版本的Jelly Bean在这里,性能优化"返回字体15的高度17
如果我将这些值设置为我的视图然后剪切一些文本,它不会显示所有内容.
在某些桌子上它看起来还不错,因为它有很大的宽度而不是在手机上.
有没有办法根据内容计算高度?
有没有办法处理视图可见性更改(例如,从GONE到VISIBLE)而不覆盖视图?
有点像View.setOnVisibilityChangeListener();?