TextView文字为什么不能居中
2014-03-03 18:16
232 查看
Paint有一个方法可以拿到FontMetrics。而FontMetrics里面有:
top ,bottom,ascent,descent,lead
他们的区别是:
为了说明,这里引用辅助线baseline=0,画文字的y坐标
top:文字的最顶端离baseline的距离(ascent的极限距离负数)
ascent:文字的上端离baseline的距离(负数)
descent:文字的下端离baseline的距离(正数)
bottom:文字最下端的距离(descent的极限距离正数)
lead:上行文字descent到下行ascent的距离
下面用图来说明:
最后总结一下比较接近的高度应该是:descent-ascent
top ,bottom,ascent,descent,lead
他们的区别是:
为了说明,这里引用辅助线baseline=0,画文字的y坐标
top:文字的最顶端离baseline的距离(ascent的极限距离负数)
ascent:文字的上端离baseline的距离(负数)
descent:文字的下端离baseline的距离(正数)
bottom:文字最下端的距离(descent的极限距离正数)
lead:上行文字descent到下行ascent的距离
下面用图来说明:
package com.sun.shine.paopao; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.util.AttributeSet; import android.view.View; public class TestFontMetrics extends View { Paint paint; Paint notePaint; public TestFontMetrics(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); paint.setAntiAlias(true); paint.setTextSize(150); notePaint = new Paint(); notePaint.setAntiAlias(true); notePaint.setTextSize(20); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int noteX = 350; int beginX = 0; int baseLine = 400; // 画文字实例 paint.setColor(Color.GREEN); canvas.drawText("jaü|攀", beginX, baseLine, paint); int baseLineColor = Color.BLACK; float width = getMeasuredWidth(); paint.setColor(baseLineColor); // 画baseLine canvas.drawLine(0, baseLine, width, baseLine, paint); notePaint.setColor(baseLineColor); canvas.drawText("baseLine", noteX, baseLine, notePaint); FontMetrics fontMetrics = paint.getFontMetrics(); float top = fontMetrics.top + baseLine; paint.setColor(Color.RED); canvas.drawLine(0, top, width, top, paint); notePaint.setColor(Color.RED); canvas.drawText("top", noteX, top, notePaint); float bottom = fontMetrics.bottom + baseLine; paint.setColor(Color.BLUE); canvas.drawLine(0, bottom, width, bottom, paint); notePaint.setColor(Color.BLUE); canvas.drawText("bottom", noteX, bottom, notePaint); float ascent = fontMetrics.ascent + baseLine; paint.setColor(Color.YELLOW); canvas.drawLine(0, ascent, width, ascent, paint); notePaint.setColor(Color.YELLOW); canvas.drawText("ascent", noteX, ascent, notePaint); float descent = fontMetrics.descent + baseLine; paint.setColor(Color.CYAN); canvas.drawLine(0, descent, width, descent, paint); notePaint.setColor(Color.CYAN); canvas.drawText("descent", noteX - 100, descent, notePaint); } }
最后总结一下比较接近的高度应该是:descent-ascent
相关文章推荐
- 横向Linearlayout中TextView文字不能水平居中的解决方法
- Android TextView文字超出一屏不能显示其它的文字 解决方案
- 如何让TextView中的文字居中显示
- Android TextView 文字居中
- 设置TextView文字居中
- iOS 之textview 的问题汇总(显示边框的问题,文字居中,placeHolder)
- android xml布局中TextView文字居中方法
- Android UI如何让TextView中的文字居中显示
- CheckedTextView怎么设置文字居中
- supportsRtl属性影响TextView的textAlignment属性,文字无法居中
- 设置TextView文字居中
- Android手机开发 控件 TextView文字居中
- 设置TextView文字居中
- TextView中的drawableleft图标不和文字一起居中
- 如何让TextView中的文字居中显示?
- (XML&Java)TextView居中&TextView中的文字居中
- Android学习 (十) 让TextView里面文字居中的方式
- TextView文字居中
- 解决android自带textview右侧文字不能对齐的问题
- Android TextView 文字居中