Android 使用单空格字体估计TextView单行中的字符

Android 使用单空格字体估计TextView单行中的字符,android,fonts,Android,Fonts,我正在尝试估计TextView中一行可以放置的字符数。其思想是获得显示宽度,然后将其除以字符宽度。(我使用的是显示宽度,因为所有获取视图宽度的方法都被破坏了) 以下是文本视图: <TextView android:id="@+id/lblNumbers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true

我正在尝试估计
TextView
中一行可以放置的字符数。其思想是获得显示宽度,然后将其除以字符宽度。(我使用的是显示宽度,因为所有获取视图宽度的方法都被破坏了)

以下是
文本视图

<TextView
    android:id="@+id/lblNumbers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />
我希望monospace字体可以使这变得容易(或更容易)

我可以检索
字体
,但似乎找不到任何方法来获取文本度量:

final TextView lblNumbers = (TextView) findViewById(R.id.lblNumbers);
if (lblNumbers != null) {
    Typeface tf = lblNumbers.getTypeface();
}

如何使用
monospace
字体确定
TextView
中使用的字符宽度?

Mike是对的-该信息在对象中可用


我不确定这是否是确定的方法,但您可以使用从
TextView\getPaint()返回的TextPaint的
measureText()
方法。
final TextView lblNumbers = (TextView) findViewById(R.id.lblNumbers);
if (lblNumbers != null) {
    Typeface tf = lblNumbers.getTypeface();
}
Float pixelWidth = 1.0f;
DisplayMetrics dm = getBaseContext().getResources().getDisplayMetrics();
if (dm != null) {
    pixelWidth = (float) dm.widthPixels;
    Log.d("PRNG", "Display width: " + pixelWidth.toString());
}

Float charWidth = 1.0f;
TextView lblNumbers = (TextView) findViewById(R.id.lblNumbers);
if (lblNumbers != null) {
    charWidth = lblNumbers.getPaint().measureText(" ");
    Log.d("PRNG", "Text width: " + charWidth.toString());
}

/* The extra gyrations negate Math.round's rounding up */
int charPerLine = Math.round(pixelWidth - 0.5f) / Math.round(charWidth - 0.5f);