所以我想在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”是一个愚蠢的尝试!
要按程序设置字体,请写。。。
TextView tv7 = new TextView(this);
tv7.setText(" TIME ");
tv7.setTypeface(Typeface.create("sans-serif-condensed",Typeface.BOLD));
tv7.setTextSize(12);
tbrow.addView(tv7);
名称“sans-serif concentrated”是从fonts.xml文件中引用的,该文件应在app-->res-->values文件夹中创建,其中包含字体。
<?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>
希望这是明确的!
一种简单的方法是在项目中添加所需的字体。
转到文件->新建->新建资源目录选择字体
这将在资源中创建一个新目录,字体。
下载字体(.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);