Java 在Android中设置固定字体

Java 在Android中设置固定字体,java,android,fonts,android-canvas,Java,Android,Fonts,Android Canvas,我有一个从1数到100的应用程序,在屏幕上显示递增计数。 我有一个自定义字体。问题是,随着数字的增加,数字不断地左右跳跃。我用graphics.Paint在画布上绘制数字 如何将字体设置为固定且不跳跃?您必须使用固定宽度字体,或者将单个数字放置在具有固定宽度的自定义布局中 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_paren

我有一个从1数到100的应用程序,在屏幕上显示递增计数。 我有一个自定义字体。问题是,随着数字的增加,数字不断地左右跳跃。我用graphics.Paint在画布上绘制数字


如何将字体设置为固定且不跳跃?

您必须使用固定宽度字体,或者将单个数字放置在具有固定宽度的自定义布局中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/digit1"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:gravity="center_horizontal|center_vertical" />

    <TextView
        android:id="@+id/digit2"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:gravity="center_horizontal|center_vertical" />
</LinearLayout>

使用自定义字体设置字体,如下所示:

private void setLogoTypeface()
{
    AssetManager assetManager = getContext().getAssets();       
    Typeface tf = Typeface.createFromAsset(assetManager,"GILB.TTF");        
    logo = (TextView)findViewById(R.id.logo);
    logo.setTypeface(tf);
}

替换您的字体名称,文本视图将呈现该字体。

您可以发布一些代码吗?听起来问题可能在于您在屏幕上定位文本的方式,而不是自定义字体。