【问题标题】:Error trying change font in Android Studio尝试在 Android Studio 中更改字体时出错
【发布时间】:2018-08-14 12:18:32
【问题描述】:

我开始从事 Android 开发,但遇到了一个问题......

我正在尝试在 Android Studio 中更改 Button 的字体,我已经密切关注了一些教程,但没有成功。

我遵循的教程:

Error trying to change font android studio

How to change the font on the TextView?

我没有收到任何错误消息,它只是不起作用。有谁知道我做错了什么?

代码:

    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/jua.ttf");

    Button Button = (Button) findViewById(R.id.Button)
    Button.setTypeface(font);

XML:

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/my_button_bg"
        android:text="Button"
        android:textColor="@android:color/white"
        android:textSize="15sp" />

文件夹结构图:
Structure of folder - image

【问题讨论】:

  • 在最新的 android studio 中有一个单独的字体文件夹,您可以在其中定义自定义字体并通过设置 font-family 添加到 textview 或按钮
  • 我建议你使用书法库来处理字体。它非常易于使用且重量轻。

标签: android android-studio


【解决方案1】:

您是否尝试过将字体插入 res/font 目录并将其加载为 @font/myfont 或 R.font.myfont。

API 26 引入了此功能,您可以在此处找到完整指南: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 2013-05-04
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多