我应用了一个自定义字体TextView,但它似乎没有改变字体。

这是我的代码:

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    myTextView.setTypeface(myTypeface);

谁能帮我摆脱这个问题?


当前回答

最简单的解决方案android支持现在!

使用自定义字体在xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/[your font resource]"/>

看详细信息:

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

其他回答

随着Android 8.0在应用程序中使用自定义字体变得容易下载字体。 我们可以直接将字体添加到项目文件夹中的res/font/文件夹中,这样一来,字体就会自动在Android Studio中可用。

现在将fontFamily属性设置为字体列表或单击更多并选择您选择的字体。这将添加tools:fontFamily="@font/your_font_file"行到你的TextView。

这将自动生成几个文件。

1. 在values文件夹中,它将创建fonts_certs.xml。

2. 在Manifest中,它会添加这样的行:

  <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" /> 

3. preloaded_fonts.xml

<resources>
    <array name="preloaded_fonts" translatable="false">
        <item>@font/open_sans_regular</item>
        <item>@font/open_sans_semibold</item>
    </array>
</resources>

我也遇到了同样的问题,TTF没有出现。我改变了字体文件,与相同的代码,它的工作。

是的,就像Dipali说的那样,下载字体非常简单。

你是这样做的……

Place a TextView. In the properties pane, select the fontFamily dropdown. If it isn't there, find the caret thingy (the > and click on it to expand textAppearance) under the. Expand the font-family drop down. In the little list, scroll all the way down till you see more fonts This will open up a dialog box where you can search from Google Fonts Search for the font you like with the search bar at the top Select your font. Select the style of the font you like (i.e. bold, normal, italic, etc) In the right pane, choose the radio button that says Add font to project Click okay. Now your TextView has the font you like!

奖金: 如果你想在你的应用程序中使用所选字体的文本样式,只需添加<item name="android:fontfamily">@font/fontnamehere</item>到你的styles.xml

七年后,你可以改变整个应用程序的textView或你想要的很容易通过使用android。支持库26++。

E.g:

创建字体包app/src/res/font,并将字体移动到其中。

在你的应用主题中添加一个fontFamily:

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

仅用于textView的示例:

<style name="fontTextView" parent="@android:style/Widget.TextView">
    <item name="android:fontFamily">monospace</item>
</style>

在你的主题中加入:

<item name="android:textViewStyle">@style/fontTextView</item>

目前它工作在8.1到4.1 API果冻豆,这是一个广泛的范围。

如果你把字体放在正确的地方,字体文件本身没有错误,你的代码应该像那样工作,RATTLESNAKE。

然而,如果你能在你的布局xml中定义一种字体,就会容易得多,就像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <!-- This text view is styled with the app theme -->
    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This uses my font in bold italic style" />

    <!-- This text view is styled here and overrides the app theme -->
    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:flFont="anotherFont"
        android:textStyle="normal"
        android:text="This uses another font in normal style" />

    <!-- This text view is styled with a style and overrides the app theme -->
    <com.innovattic.font.FontTextView
        style="@style/StylishFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This also uses another font in normal style" />

</LinearLayout>

随附res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

    <!-- Application theme -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    </style>

    <!-- Style to use for ALL text views (including FontTextView) -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView">
        <item name="android:textAppearance">@style/MyTextAppearance</item>
    </style>

    <!-- Text appearance to use for ALL text views (including FontTextView) -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo">
        <!-- Alternatively, reference this font with the name "aspergit" -->
        <!-- Note that only our own TextView's will use the font attribute -->
        <item name="flFont">someFont</item>
        <item name="android:textStyle">bold|italic</item>
    </style>

    <!-- Alternative style, maybe for some other widget -->
    <style name="StylishFont">
        <item name="flFont">anotherFont</item>
        <item name="android:textStyle">normal</item>
    </style>

</resources>

为此,我专门创建了几个工具。参考GitHub上的这个项目,或者看看这篇解释了整个事情的博客文章。