【问题标题】:Android set Roboto font with bold, italic, regular,... (something like custom font family)Android将Roboto字体设置为粗体,斜体,常规,......(类似于自定义字体系列)
【发布时间】:2012-03-02 10:18:30
【问题描述】:

我知道如何在 Android 应用程序中以编程方式设置自定义字体。 有没有办法为自定义字体(资产)加载字体,Android框架将使用基于粗体、斜体等的正确文件?

例如,现在我正在尝试将 Roboto 字体设置为 TextView

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);

它工作正常。但是由于我在 xml 布局中将 TextView 设置为粗体,因此文本没有加粗

<TextView
    android:id="@+id/my_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="50dp"
    android:textStyle="bold"
    android:gravity="center"
    android:text="@string/my_text"
    android:textColor="@color/my_foreground"
    android:textSize="24dp" />

如何正确地从资产中加载字体才能正常工作?

textView.setTypeface(typeface, Typeface.BOLD);

在我的资产目录中只有一个“字体系列”

Roboto-Black.ttf
Roboto-BlackItalic.ttf
Roboto-Bold.ttf
Roboto-BoldCondensed.ttf
Roboto-BoldCondensedItalic.ttf
Roboto-BoldItalic.ttf
Roboto-Condensed.ttf
Roboto-CondensedItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Medium.ttf
Roboto-MediumItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf

如何在一个字体/系列中加载所有字体?

【问题讨论】:

    标签: android fonts typeface font-family


    【解决方案1】:

    不确定发布链接是否是个好主意,但无论如何... 这是我关于这个主题的博客文章,它有一个完全解决这个问题的类。 http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/

    【讨论】:

    • 看起来不错,但格式已损坏。为什么不在这里粘贴相同的类?
    • 链接失效
    • 是的,抱歉,该网站已经关闭多年了。您现在可以使用 android 数据绑定来使用任何类型的字体
    【解决方案2】:

    我不知道有一种方法可以将单一字体的不同字体变体视为一个系列,但是字体往往具有较大的文件大小,因此您可能无论如何都不想将所有这些字体导入您的应用程序。相反,您可以只使用 Medium 字体,然后为其设置粗体和斜体属性。

    例如,如果您在 XML 布局中设置了 android:textStyle="bold",则可以在代码中执行此操作以保持粗体样式:

    Typeface currentTypeFace = textView.getTypeface();
    if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) {
        textView.setTypeface(tf, Typeface.BOLD);
    } else {
        textView.setTypeface(tf);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2016-12-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      相关资源
      最近更新 更多