如何在Android中更改TextView的fontFamily

本文翻译自:How to change fontFamily of TextView in Android

So I'd like to change the android:fontFamily in Android but I don't see any pre-defined fonts in Android. 所以我想在Android中更改android:fontFamily ,但在Android中看不到任何预定义的字体。 How do I select one of the pre-defined ones? 如何选择一种预定义的? I don't really need to define my own TypeFace but all I need is something different from what it shows right now. 我实际上不需要定义自己的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"
 />

It seems what I did up there won't really work! 看来我在那儿所做的并不能真正起作用! BTW android:fontFamily="Arial" was a stupid attempt! 顺便说一句android:fontFamily="Arial"是一个愚蠢的尝试!


#1楼

参考:https://stackoom.com/question/ot8F/如何在Android中更改TextView的fontFamily


#2楼

What you want is not possible. 您想要的是不可能的。 You must need to set TypeFace in your Code. 您必须在代码中设置TypeFace

In XML what you can do is XML ,您可以做的是

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

other then this you can not play much with the Fonts in XML. 否则,您将无法在XML中使用字体。 :) :)

For Arial you need to set type face in your code. 对于Arial您需要在代码中设置type face。


#3楼

This is the way to set the font programmatically: 这是通过编程方式设置字体的方法:

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

put the font file in your assets folder. 将字体文件放在资产文件夹中。 In my case I created a subdirectory called fonts. 就我而言,我创建了一个名为fonts的子目录。

EDIT: If you wonder where is your assets folder see this question 编辑:如果您想知道您的资产文件夹在哪里,请参阅此问题


#4楼

It's the same as android:typeface . android:typeface相同。

built-in fonts are: 内置字体有:

  • normal 正常
  • sans
  • serif 衬线
  • monospace 等宽

See android:typeface . 参见android:typeface


#5楼

Android doesn't allow you to set custom fonts from the XML layout. Android不允许您从XML布局中设置自定义字体。 Instead, you must bundle the specific font file in your app's assets folder, and set it programmatically. 相反,您必须将特定字体文件捆绑在应用程序的资产文件夹中,并以编程方式进行设置。 Something like: 就像是:

TextView textView = (TextView) findViewById(<your TextView ID>);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
textView.setTypeface(typeFace);

Note that you can only run this code after setContentView() has been called. 请注意,只有在调用setContentView()之后才能运行此代码。 Also, only some fonts are supported by Android, and should be in a .ttf (TrueType) or .otf (OpenType) format. 另外,Android仅支持某些字体,并且应采用.ttf (TrueType).otf (OpenType)格式。 Even then, some fonts may not work. 即使那样,某些字体也可能不起作用。

This is a font that definitely works on Android, and you can use this to confirm that your code is working in case your font file isn't supported by Android. 是一种绝对可以在Android上使用的字体,如果Android不支持您的字体文件,您可以使用它来确认您的代码可以正常工作。

Android O Update: This is now possible with XML in Android O , based on Roger's comment. Android O更新:基于Roger的评论,现在可以使用Android O中的XML进行更新。


#6楼

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available: 从android 4.1 / 4.2 / 5.0起,可以使用以下Roboto字体系列:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black"     // roboto black
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

在此处输入图片说明

in combination with 与...结合

android:textStyle="normal|bold|italic"

this 16 variants are possible: 这16种变体是可能的:

  • Roboto regular 机械手常规
  • Roboto italic 斜体
  • Roboto bold Roboto粗体
  • Roboto bold italic Roboto粗体斜体
  • Roboto-Light 机器人光
  • Roboto-Light italic Roboto-Light斜体
  • Roboto-Thin 机械薄型
  • Roboto-Thin italic Roboto-Thin斜体
  • Roboto-Condensed 浓缩的机器人
  • Roboto-Condensed italic Roboto压缩斜体
  • Roboto-Condensed bold 机械手浓缩黑体
  • Roboto-Condensed bold italic Roboto压缩的粗体斜体
  • Roboto-Black 机器人黑
  • Roboto-Black italic Roboto-Black斜体
  • Roboto-Medium 机械人
  • Roboto-Medium italic 机械斜体

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="font_family_light">sans-serif-light</string>
    <string name="font_family_medium">sans-serif-medium</string>
    <string name="font_family_regular">sans-serif</string>
    <string name="font_family_condensed">sans-serif-condensed</string>
    <string name="font_family_black">sans-serif-black</string>
    <string name="font_family_thin">sans-serif-thin</string>
</resources>
发布了0 篇原创文章 · 获赞 75 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105472246