【问题标题】:How to apply two different font to a TextView?如何将两种不同的字体应用于 TextView?
【发布时间】:2020-03-26 11:50:43
【问题描述】:

我的情况和Android一样——三句话,两种风格,一种TextView。唯一的区别是我想在整个文本上设置自定义字体。我已将 roboto Font 作为资产包含在我的项目中,并希望将该字体应用到 TextView,文本的第一部分将是 roboto_LIGHT,中间部分是 roboto_NORMAL 和剩余部分 roboto_LIGHT 再次。有什么建议吗?

需要以下格式的文本。具有不同样式和单个文本视图的自定义文本。

【问题讨论】:

    标签: java android fonts textview styles


    【解决方案1】:

    您可以使用一些html标签,如<b>,使用以下代码表示粗体:

    textView.setText(Html.fromHtml("Login to the <b>Web Wallet</b> on PC or MAC"));
    

    然后您可以定义您的自定义字体系列,如How to create custom font family with bold font

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            app:fontStyle="normal"
            app:fontWeight="400"
            app:font="@font/roboto_LIGHT" />
        <font
            app:fontStyle="normal"
            app:fontWeight="700"
            app:font="@font/roboto_NORMAL" />
    </font-family>
    

    700 是粗体的字体。然后你应该将创建的字体系列应用到 TextView:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fontFamily="@font/my_custom_font"/>
    

    或者您可以在 ConstraintLayout 中添加 3 个 TextView,并给它们一些约束以使其处于您想要的位置。

    【讨论】:

    • 谢谢你的帮助,我写的和你写的完全一样,但字体没有改变,它只显示正常的粗体,但不显示 roberto light
    • 如果您看到一些粗体文本,无论出于何种原因,它似乎都没有加载该字体系列。尝试在 TextView 中使用 app:fontFamily 而不是 android:fontFamily。或者尝试两者,然后如果它有效,您可以删除不适用的一个
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多