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

这是我的代码:

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

谁能帮我摆脱这个问题?


当前回答

你可以在https://github.com/neopixl/PixlUI上使用PixlUI

导入他们的.jar并在XML中使用它

 <com.neopixl.pixlui.components.textview.TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    pixlui:typeface="GearedSlab.ttf" />

其他回答

确保在调用super和setContentView()之后将上述代码粘贴到onCreate()中。这个小细节让我挂念了一会儿。

我知道已经有了很好的答案,但这里有一个完全工作的实现。

这是自定义文本视图:

package com.mycompany.myapp.widget;

/**
 * Text view with a custom font.
 * <p/>
 * In the XML, use something like {@code customAttrs:customFont="roboto-thin"}. The list of fonts
 * that are currently supported are defined in the enum {@link CustomFont}. Remember to also add
 * {@code xmlns:customAttrs="http://schemas.android.com/apk/res-auto"} in the header.
 */
public class CustomFontTextView extends TextView {

    private static final String sScheme =
            "http://schemas.android.com/apk/res-auto";
    private static final String sAttribute = "customFont";

    static enum CustomFont {
        ROBOTO_THIN("fonts/Roboto-Thin.ttf"),
        ROBOTO_LIGHT("fonts/Roboto-Light.ttf");

        private final String fileName;

        CustomFont(String fileName) {
            this.fileName = fileName;
        }

        static CustomFont fromString(String fontName) {
            return CustomFont.valueOf(fontName.toUpperCase(Locale.US));
        }

        public Typeface asTypeface(Context context) {
            return Typeface.createFromAsset(context.getAssets(), fileName);
        }
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (isInEditMode()) {
            return;
        } else {
            final String fontName = attrs.getAttributeValue(sScheme, sAttribute);

            if (fontName == null) {
                throw new IllegalArgumentException("You must provide \"" + sAttribute + "\" for your text view");
            } else {
                final Typeface customTypeface = CustomFont.fromString(fontName).asTypeface(context);
                setTypeface(customTypeface);
            }
        }
    }
}

这里是自定义属性。这应该进入你的res/attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

下面是你如何使用它。我将使用一个相对布局来包装它并显示customAttr声明,但它显然可以是您已经拥有的任何布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:customAttrs="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mycompany.myapp.widget.CustomFontTextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="foobar"
         customAttrs:customFont="roboto_thin" />

</RelativeLayout>

在Mobiletuts+上有一个关于Android文本格式的很好的教程。快速提示:自定义Android字体

编辑:现在我自己测试了一下。这是解决方案。你可以使用一个子文件夹称为字体,但它必须在资产文件夹,而不是在res文件夹。所以

资产/字体

还要确保字体结尾。我是说字体文件本身的结尾都是小写。换句话说,它不应该是myfont。ttf,而应该是myfont。ttf,这种方式必须小写

在尝试了这篇文章中描述的大多数解决方案后,我偶然发现了Calligraphy (https://github.com/chrisjenx/Calligraphy) - Christopher Jenkins的一个库,可以让你轻松地向应用程序添加自定义字体。与这里建议的方法相比,他的库的优点是:

你不必引入自己的重载TextView组件,你使用内置的TextView 您可以使用gradle轻松地包含该库 图书馆不限制你的字体选择;您只需将您喜欢的添加到资产目录 你不仅得到自定义文本视图-所有其他基于文本的Android组件也将显示使用您的自定义字体。

您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。

而不是

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

简单:

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));

这个库还提供了以下字体面。

Roboto Droid衬线 机器人机器人 自由 有趣的栽培者 Android的国家 绿色的鳄梨 识别