有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中的java设置固定字体

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

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


共 (2) 个答案

  1. # 1 楼答案

    你要么使用固定宽度的字体,要么将单个数字放在固定宽度的自定义布局中

    <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>
    
  2. # 2 楼答案

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

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

    替换字体名称,文本视图将呈现字体