所以我想在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”是一个愚蠢的尝试!


当前回答

如果您使用的是Android Studio 3.5+,更改字体非常简单。在“设计”视图上选择文本小部件,并在“属性窗口”上检查字体系列。值下拉列表包含所有可用字体,您可以从中选择一种字体。如果您正在查找Google字体,请单击“更多字体”选项。

属性窗口

谷歌字体

其他回答

从Android Studio 3.0开始,它非常容易更改字体系列

使用支持库26,它将在运行Android API版本16和更高版本的设备上运行

在res目录下创建一个文件夹字体。下载你想要的字体并将其粘贴到字体文件夹中。结构应该如下所示

注意:从Android Support Library 26.0开始,您必须声明两组属性(Android:和app:),以确保在运行的设备上加载字体Api 26或更低。

现在,您可以使用

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/dancing_script"
app:fontFamily="@font/dancing_script"/>

以程序方式更改

 Typeface typeface = getResources().getFont(R.font.myfont);
   //or to support all versions use
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
 textView.setTypeface(typeface);  

要使用styles.xml更改字体,请创建样式

 <style name="Regular">
        <item name="android:fontFamily">@font/dancing_script</item>
        <item name="fontFamily">@font/dancing_script</item>
        <item name="android:textStyle">normal</item>
 </style>

并将此样式应用于TextView

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Regular"/>

也可以创建自己的字体系列

-右键单击字体文件夹并转到“新建”>“字体资源文件”。此时将显示“新建资源文件”窗口。

-输入文件名,然后单击“确定”。新的字体资源XML将在编辑器中打开。

例如,在这里编写自己的字体系列

<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

这只是将特定的fontStyle和fontWeight映射到将用于呈现该特定变体的字体资源。fontStyle的有效值为normal或italic;fontWeight符合CSS字体权重规范

1.要更改布局中的字体族,可以编写

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/lobster"/>

2.程序性改变

 Typeface typeface = getResources().getFont(R.font.lobster);
   //or to support all versions use
Typeface typeface = ResourcesCompat.getFont(context, R.font.lobster);
 textView.setTypeface(typeface);  

要更改整个应用程序的字体,请在AppTheme中添加这两行

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:fontFamily">@font/your_font</item>
     <item name="fontFamily">@font/your_font</item>
  </style>

有关更多信息,请参阅文档,Android自定义字体教程

如果您想在许多地方使用具有相同字体系列的TextView,请扩展TextView类并按如下方式设置字体:-

public class ProximaNovaTextView extends TextView {
    public ProximaNovaTextView(Context context) {
        super(context);
        applyCustomFont(context);
    }
    public ProximaNovaTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        applyCustomFont(context);
    }
    public ProximaNovaTextView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       applyCustomFont(context);
    } 
    private void applyCustomFont(Context context) {
        Typeface customFont = FontCache.getTypeface("proximanova_regular.otf", context);
        setTypeface(customFont);
    }
}

然后对TextView使用XML中的这个自定义类,如下所示:-

<com.myapp.customview.ProximaNovaTextView
android:id="@+id/feed_list_item_name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
/>

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

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" />

它重量轻,易于实现,只需在asset文件夹中复制字体并在xml中使用名称即可。

新字体资源允许使用

android:fontFamily="@font/my_font_in_font_folder"

从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>