字体真棒 ttf 文件在安卓字体家族中不起作用



我正在开发安卓应用程序,我下载了字体真棒 ttf 文件并添加到安卓工作室项目中。它不起作用,但如果我尝试使用 icoomon工作正常。谁能帮帮我。

法典:

<TextView
            android:text="aws"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/button" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            app:fontFamily="@font/fa_brands_400"/>
首先,

确保将.ttf下载的文件放在Asset文件夹中。

然后创建一个名为 FontAwesome 的类,它像这样扩展TextView

  public class FontAwesome extends TextView {

public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}
public FontAwesome(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
public FontAwesome(Context context) {
    super(context);
    init();
}
private void init() {
//Font name should not contain "/".
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "fontawesome.ttf");
    setTypeface(tf);
}

}

最后,您按如下方式创建TextView

<PACKAGE_NAME.Fontawesome
android:id="@+id/userLogin"
android:text="&#xf007;  Login Now"
android:clickable="true"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

我希望这对你有所帮助。

最新更新