我试图使我的TextPaint看起来完全像TextView,但是TextView的文本总是比用TextPaint绘制的文本大一些。
TextView文字:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:typeface="serif"
    android:text="some text"
     />

TextPaint文字:
    int textSize = 20;
    paint = new TextPaint();
    paint.setTextSize(textSize*density);
    paint.setTypeface(Typeface.SERIF);
    paint.setColor(Color.BLACK);

最佳答案

您必须将大小转换为第二个清单中的像素:

int size = 20; //20dp

int pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                          size, getResources().getDisplayMetrics());

10-07 19:18
查看更多