如何在Android中更改TextView的字体族?

834

我希望更改Android中的android:fontFamily,但是我没有在Android中看到任何预定义的字体。我该如何选择其中一个预定义的字体?我不需要定义自己的Typeface,但我需要一些与当前显示内容不同的东西。

<TextView
    android:id="@+id/HeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp"
    android:gravity="center"
    android:text="CallerBlocker"
    android:textSize="40dp"
    android:fontFamily="Arial"
 />

看起来我之前做的那个不会真正起作用!顺便说一下,android:fontFamily="Arial" 是一个愚蠢的尝试!


请查看此链接 https://dev59.com/33E95IYBdhLWcg3wSb7P#48642116 - duggu
39个回答

1
您可以在res/layout/value/style.xml中设置样式,如下所示:

<style name="boldText">
    <item name="android:textStyle">bold|italic</item>
    <item name="android:textColor">#FFFFFF</item>
</style>

并在 main.xml 文件中使用此样式:

style="@style/boldText"

1

有一个很好的库可用于此

    implementation 'uk.co.chrisjenx:calligraphy:2.3.0'

此库用于更改整个应用程序中所有视图的字体。但是,它不适用于适配器视图,如列表视图。因此,我们需要在每个适配器中专门添加代码。 - Senthilvel S

1

或许我的评论对某些人会有用:我一直在尝试弄清楚为什么app:fontFamily="@font/my_font"无法工作,浪费了一些时间后发现可行的解决方案是android:fontFamily="@font/my_font"


1

程序化地向TextView添加字体的最简单方法是首先将字体文件添加到项目中的资产文件夹中。例如,您的字体路径看起来像这样:assets/fonts/my_font.otf

然后将其添加到一个TextView中:

Kotlin

val font_path = "fonts/my_font.otf"  

myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)

textView.typeface = myTypeface

Java

String font_path = "fonts/my_font.otf";
Typeface myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)
textView.setTypeface(myTypeface);

0

尝试以下简单步骤。 1. 在res文件夹中创建字体文件夹。 2. 将.ttf文件复制并粘贴到字体文件夹中。 3. 现在在xml中像下面这样给出路径。

 android:fontFamily="@font/frutiger"

或者你的文件名是什么。 就这样,愉快地编码吧


0
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedAttribute">
    <font
        android:font="@font/google_sans_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/google_sans_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/google_sans_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/google_sans_italic"
        app:fontStyle="italic"
        app:fontWeight="400" />

    <font
        android:font="@font/google_sans_medium"
        android:fontStyle="normal"
        android:fontWeight="500"
        app:font="@font/google_sans_medium"
        app:fontStyle="normal"
        app:fontWeight="500" />

    <font
        android:font="@font/google_sans_medium_italic"
        android:fontStyle="italic"
        android:fontWeight="500"
        app:font="@font/google_sans_medium_italic"
        app:fontStyle="italic"
        app:fontWeight="500" />

    <font
        android:font="@font/google_sans_bold"
        android:fontStyle="normal"
        android:fontWeight="600"
        app:font="@font/google_sans_bold"
        app:fontStyle="normal"
        app:fontWeight="600" />

    <font
        android:font="@font/google_sans_bold_italic"
        android:fontStyle="italic"
        android:fontWeight="600"
        app:font="@font/google_sans_bold_italic"
        app:fontStyle="italic"
        app:fontWeight="600" />

</font-family>

0

这里可以查看所有可用的字体系列值及其对应的字体文件名称(此文件用于Android 5.0+)。在移动设备上,您可以在以下位置找到它:

/system/etc/fonts.xml (for 5.0+)

(针对 Android 4.4 及以下版本使用 this 版本,但我认为 fonts.xml 具有更清晰的格式且易于理解。)
(例如,)
    <!-- first font is default -->
20    <family name="sans-serif">
21        <font weight="100" style="normal">Roboto-Thin.ttf</font>
22        <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
23        <font weight="300" style="normal">Roboto-Light.ttf</font>
24        <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
25        <font weight="400" style="normal">Roboto-Regular.ttf</font>
26        <font weight="400" style="italic">Roboto-Italic.ttf</font>
27        <font weight="500" style="normal">Roboto-Medium.ttf</font>
28        <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
29        <font weight="900" style="normal">Roboto-Black.ttf</font>
30        <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
31        <font weight="700" style="normal">Roboto-Bold.ttf</font>
32        <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
33    </family>

family标签中定义的名称属性name="sans-serif"指定了您可以在android:fontFamily中使用的值。

font标签定义了对应的字体文件。

在这种情况下,您可以忽略<!-- fallback fonts -->下方的源代码,它用于字体备选逻辑。


0

我使用Letter Press lib来处理非TextView的东西,比如按钮,而对于TextView,我使用kianoni fontloader lib,因为这个库中的样式使用比Letter Press更容易,而且我得到了理想的反馈。 这对于那些想要使用自定义字体(除了Roboto字体)的人来说非常棒。 这就是我使用字体库的经验。 对于那些想要使用自定义类来更改字体的人,我强烈推荐使用这个代码片段创建这个类。

public class TypefaceSpan extends MetricAffectingSpan {
    /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
            new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link android.graphics.Typeface} and apply to a {@link android.text.Spannable}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                    .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
} 

并且像这样使用类:

AppData = PreferenceManager.getDefaultSharedPreferences(this);
TextView bannertv= (TextView) findViewById(R.id.txtBanner);
    SpannableString s = new SpannableString(getResources().getString(R.string.enterkey));
    s.setSpan(new TypefaceSpan(this, AppData.getString("font-Bold",null)), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    bannertv.setText(s);

或许这可以帮助你。


0

您可以使用以下库来轻松完成它

https://github.com/sunnag7/FontStyler

<com.sunnag.fontstyler.FontStylerView
              android:textStyle="bold"
              android:text="@string/about_us"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingTop="8dp"
              app:fontName="Lato-Bold"
              android:textSize="18sp"
              android:id="@+id/textView64" />

它非常轻量级且易于实现,只需将您的字体复制到资产文件夹中并在XML中使用名称即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接