如何在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个回答

23

一种简单的方法是在项目中添加所需的字体

前往文件->新建->新建资源目录

选择 字体

这将在您的资源中创建一个名为font的新目录。

下载所需的字体(.ttf)。我使用https://fonts.google.com 来下载。

将其添加到您的fonts文件夹中,然后可以在 XML 或以编程方式中使用它们。

XML -

<TextView 
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/your_font"/>

以编程方式 -

 Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
 textView.setTypeface(typeface); 

15
建议使用ResourcesCompat.getFont方法。 - Vadim Kotov

20

我认为可能已经晚了,但这个解决方案或许对其他人有帮助。如果要使用自定义字体,请将字体文件放置在您的字体目录中。

textView.setTypeface(ResourcesCompat.getFont(this, R.font.lato));

15

我正在使用由Chris Jenx设计的优秀库Calligraphy,它允许您在Android应用程序中使用自定义字体。不妨试试!


是的,但是例如我想要使用它的功能,但不想实现整个库;) - Morozov

12

你所希望的是不可能实现的。你必须在你的代码中设置TypeFace

XML中,你可以做的是

android:typeface="sans" | "serif" | "monospace"

除此之外,在XML中你并不能做太多关于字体的操作。 :)

对于 Arial ,你需要在代码中设置字体类型。


12

通过资源声明字体是管理字体的简单方法,如下所示:

<!--++++++++++++++++++++++++++-->
<!--added on API 16 (JB - 4.1)-->
<!--++++++++++++++++++++++++++-->
<!--the default font-->
<string name="fontFamily__roboto_regular">sans-serif</string>
<string name="fontFamily__roboto_light">sans-serif-light</string>
<string name="fontFamily__roboto_condensed">sans-serif-condensed</string>

<!--+++++++++++++++++++++++++++++-->
<!--added on API 17 (JBMR1 - 4.2)-->
<!--+++++++++++++++++++++++++++++-->
<string name="fontFamily__roboto_thin">sans-serif-thin</string>

<!--+++++++++++++++++++++++++++-->
<!--added on Lollipop (LL- 5.0)-->
<!--+++++++++++++++++++++++++++-->
<string name="fontFamily__roboto_medium">sans-serif-medium</string>
<string name="fontFamily__roboto_black">sans-serif-black</string>
<string name="fontFamily__roboto_condensed_light">sans-serif-condensed-light</string>

这是基于此处此处的源代码。


在哪里声明它们? - AZ_
@AZ_ 就像许多资源文件一样,您可以将其放置在任何XML文件中,放置在“res/values/”文件夹内。例如,将其放置在“res/values/fonts.xml”中。要使用它,只需像这样简单地执行:android:fontFamily="string/fontFamily__roboto_regular"。 - android developer
谢谢,我正在使用这个 https://github.com/norbsoft/android-typeface-helper ,它非常有帮助。 - AZ_
好的,这个库可能是用于编程实现的。这里是针对XML的。 - android developer

10
如果您正在使用Android Studio 3.5+,更改字体非常简单。在Design视图中选择文本小部件并检查Attribute窗口中的字体系列。值下拉菜单包含所有可用的字体,您可以从中选择一个。如果您想要Google字体,请点击更多字体选项。
属性窗口 Attribute Window Google字体 Google Fonts

这个答案在2020年应该更加顶尖。 - mcy

9

你可以通过以下方式动态设置字体族类似于xml中的android:fontFamily:

For Custom font:

 TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
 Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf"); 
 tv.setTypeface(face);

For Default font:

 tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));

这是一组默认字体族列表,可以通过将双引号字符串 "sans-serif-medium" 替换为其中任何一个来使用。
FONT FAMILY                    TTF FILE                    

1  casual                      ComingSoon.ttf              
2  cursive                     DancingScript-Regular.ttf   
3  monospace                   DroidSansMono.ttf           
4  sans-serif                  Roboto-Regular.ttf          
5  sans-serif-black            Roboto-Black.ttf            
6  sans-serif-condensed        RobotoCondensed-Regular.ttf 
7  sans-serif-condensed-light  RobotoCondensed-Light.ttf   
8  sans-serif-light            Roboto-Light.ttf            
9  sans-serif-medium           Roboto-Medium.ttf           
10  sans-serif-smallcaps       CarroisGothicSC-Regular.ttf 
11  sans-serif-thin            Roboto-Thin.ttf             
12  serif                      NotoSerif-Regular.ttf       
13  serif-monospace            CutiveMono.ttf              

"

“mycustomfont.ttf”是ttf文件。路径将在src/assets/fonts/mycustomfont.ttf中,你可以在这个默认字体系列中了解更多关于默认字体的信息。

"

8
您也可以通过在res目录下添加一个字体文件夹来实现此操作,如下所示。

enter image description here

然后,选择字体作为资源类型。 输入图片描述 你可以从https://www.1001fonts.com/找到可用的字体,然后提取TTF文件到这个字体目录中。

enter image description here

最后,只需更改包含您的TextView的XML文件,通过添加android:fontFamily:"@font/urfontfilename"来实现。

enter image description here


非常好,感谢您的帮助。我不知道为什么其他人有更多的星星,但是您的已经确认可以与Material Design文本视图一起使用,您必须使用app:fontFamily=,然而,其他所有内容都是相同的。 - EvOlaNdLuPiZ
你救了我的命,我刚创建了一个名为“font”的文件夹,但它不起作用。无论如何,我使用了您的方法,它起作用了。谢谢! - Hilal

6

通过一些尝试和错误,我学到了以下内容。

在*.xml文件中,您可以将常规字体与以下功能结合使用,而不仅仅是字体:

 android:fontFamily="serif" 
 android:textStyle="italic"

有了这两种样式,就不需要在任何其他情况下使用字体。使用fontfamily&textStyle,组合的范围更加广泛。


6

试试这个:

TextView textview = (TextView) findViewById(R.id.textview);
Typeface tf= Typeface.createFromAsset(getAssets(),"fonts/Tahoma.ttf");
textview .setTypeface(tf);

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