所以我想在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内部字体的地狱即将结束,因为今年在Google IO上,我们终于得到了这个->https://developer.android.com/preview/features/working-with-fonts.html

现在有了一个新的资源类型字体,您可以将所有应用程序字体放在res/fonts文件夹中,然后使用R.font.my_custom_font进行访问,就像您可以访问字符串res值、可绘制res值等一样。您甚至有机会创建字体face xml文件,该文件将由您的自定义字体设置(关于斜体、粗体和下划线属性)。

阅读上面的链接了解更多信息。让我们看看支持。

其他回答

我使用Letter Press库来处理我的非文本视图内容,如Buttons和kianoni fontloader库处理我的文本视图,因为在这个库中使用样式比Letter Press更容易,我得到了理想的反馈。这对于那些想要使用除Roboto字体之外的自定义字体的人来说非常有用。这是我使用字体库的经验。对于那些希望使用自定义类更改字体的人,我强烈建议使用以下代码段创建此类

public class TypefaceSpan extends MetricAffectingSpan {
    /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
            new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link android.graphics.Typeface} and apply to a {@link android.text.Spannable}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                    .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
} 

并像这样使用类:

AppData = PreferenceManager.getDefaultSharedPreferences(this);
TextView bannertv= (TextView) findViewById(R.id.txtBanner);
    SpannableString s = new SpannableString(getResources().getString(R.string.enterkey));
    s.setSpan(new TypefaceSpan(this, AppData.getString("font-Bold",null)), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    bannertv.setText(s);

也许这有帮助。

Android不允许您从XML布局设置自定义字体。相反,您必须将特定字体文件绑定到应用程序的assets文件夹中,并以编程方式进行设置。类似于:

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

注意,只有在调用setContentView()后才能运行此代码。此外,Android只支持某些字体,并且应为.ttf(TrueType)或.otf(OpenType)格式。即使如此,某些字体也可能不起作用。

这是一种在Android上绝对有效的字体,如果Android不支持您的字体文件,您可以使用它来确认您的代码是否有效。

Android O更新:根据Roger的评论,这现在可以在Android O中使用XML。

从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自定义字体教程

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

转到文件->新建->新建资源目录选择字体

这将在资源中创建一个新目录,字体。

下载字体(.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); 

您也可以在res目录下添加字体文件夹,如下所示。

然后,选择字体作为资源类型。

您可以从https://www.1001fonts.com/,然后将TTF文件提取到此字体目录。

最后,只需通过添加android:fontFamily:“@font/urfontfilename”来更改包含文本视图的XML文件