所以我想在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"
/>
看来我在上面做的事真的行不通!BTW android:fontFamily=“Arial”是一个愚蠢的尝试!
一种简单的方法是在项目中添加所需的字体。
转到文件->新建->新建资源目录选择字体
这将在资源中创建一个新目录,字体。
下载字体(.ttf)。我使用https://fonts.google.com同样的
将其添加到字体文件夹中,然后在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);
在某些情况下,这里有一种更简单的方法。其原理是在xml布局中添加不可见的TextVview,并在java代码中获取其typeFace。
xml文件中的布局:
<TextView
android:text="The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty."
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="sans-serif-thin"
android:id="@+id/textViewDescription"/>
java代码:
myText.setTypeface(textViewSelectedDescription.getTypeface());
它对我很有用(例如在TextSwitcher中)。
从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)
与
android:textStyle="normal|bold|italic"
这16种变体是可能的:
Roboto常规Roboto斜体Roboto粗体Roboto粗体斜体Roboto灯Roboto浅斜体Roboto薄型Roboto细斜体Roboto浓缩Roboto浓缩斜体Roboto浓缩粗体Roboto浓缩粗体斜体Roboto黑色Roboto黑色斜体Roboto中等Roboto中斜体
字体.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>
在这里,您可以看到所有可用的字体系列值及其对应的字体文件名(该文件在android 5.0+中使用)。在移动设备上,您可以在以下位置找到它:
/system/etc/fonts.xml (for 5.0+)
(对于使用此版本的android 4.4及以下版本,但我认为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属性name=“sans-serif”定义了可以在android中使用的值:fontFamily。
字体标记定义对应的字体文件。
在这种情况下,可以忽略<!--下的源代码回退字体-->,用于字体的回退逻辑。