Android:FontMetrics详解
16lz
2021-12-04
FontMetrics在API中解释如下:
Class that describes the various metrics for a font at a given text size. Remember, Y values increase going down, so those values will be positive, and values that measure distances going up will be negative. This class is returned by getFontMetrics().
向下为正,向上为负,以baseline为基准
下面通过两幅图直观感受一下:(第一幅图讲解哪些参数究竟指的哪部分,第二幅图演示了对应的大致的值)
所以,要想让单元格中的文字居中,需要如下代码:
float y = height/2-(fMetrics.ascent+fMetrics.descent)/2;
height是指单元格的高度。中间为什么是减号?是因为(fMetrics.ascent+fMetrics.descent)/2是负,再减一下就是加。这样就可以把文本放在中间。(Android的Y轴是向下的,height/2在红线上面,再加上红线到base的距离,就差不多在中间。如果误认为Y轴向上,是解释不通的)
更多相关文章
- Unity3d 调用Android震动
- Android通过Mainfest设置Theme实现布局全屏
- Android(安卓)发送短信功能
- Android(安卓)ProgressBar 进度条
- android自定义带图片的title
- SeekBar自定义
- Failed to fetch URL https://dl-ssl.google.com/android/reposi
- Android(安卓)Studio下“Error:Could not find com.android.tool
- Android:unable to start service Intent {act = com.android.ema