1.TextView 不仅能显示文字,还能显示图片.
StringBuilder sb = new StringBuilder();
sb.append(" ");
sb.append(tv.getText().toString());
SpannableStringBuilder sp = new SpannableStringBuilder(sb.toString());
Drawable drawable=context.getResources().getDrawable(R.drawable.icon);
int drawableHeight=getFontHeight(tv.getTextSize())*8/10;
drawable.setBounds(0, 0, drawableHeight, drawableHeight);
sp.setSpan(new ImageSpan(drawable), 0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(sp);
这样就在TextView上显出出了图片.
2.操作文本的高度与宽度.
Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标
FontMetrics对象
它以四个基本坐标为基准,分别为:
・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent
・FontMetrics.bottom
获取文字的高度.
Paint paint = new Paint();
paint.setTextSize(fontSize);
FontMetrics fm = paint.getFontMetrics();
Math.ceil(fm.descent - fm.ascent);
获取字符串的宽度
Paint paint = new Paint();
paint.setTextSize(fontSize);
float witth=paint.measureText(str);